V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
mahonejolla
V2EX  ›  Python

求个 idea, Python 等数量压缩文件

  •  
  •   mahonejolla · 2019-05-21 16:46:54 +08:00 · 1424 次点击
    这是一个创建于 1773 天前的主题,其中的信息可能已经有所发展或是发生改变。
    例子是,我这有一个文件夹,这个文件夹里面有几万个图片,都是有序列的。现在想每 1000 张图片压缩成一个压缩包。最后一次不足 1000 张同样压缩成一个包。这个该怎么实现呢?如果是你,你会采取什么方法呢?
    第 1 条附言  ·  2019-05-21 20:46:40 +08:00

    现在是将文件等数量复制到每一个文件夹。暂时不会怎么压缩目录下的每一个文件夹。

    # -*- coding: utf-8 -*-
    import os
    import shutil
    
    #path of imgr
    path = 'G:/file/201904012-20190418-GKJ2/BACK/zz22'
    
    #path of folder
    folderPath = 'G:/file/201904012-20190418-GKJ2/BACK/ZZZZZZZZ'
    #give the img list
    file_list = sorted(os.listdir(path))
    files_each_folder=1000           #设置每个文件夹里面的文件数目
    folder_number = int((len(file_list)+files_each_folder-1)/files_each_folder)   #文件夹数目向上取整
    sort_folder_number = [x for x in range(0,folder_number)]
    prefix_folder_back='IMAGE_PATCH_BACK_PART'   #反面前缀
    prefix_folder_front='IMAGE_PATCH_BACK_PART'   #正面前缀
    # 创建文件夹
    for number in sort_folder_number:
        new_folder_path = os.path.join(folderPath,prefix_folder_back+'%s'%number)#new_folder_path is ‘folderPath\number'
    
        if not os.path.exists(new_folder_path):
            os.makedirs(new_folder_path)
            print("new a floder named "+str(number)+'at the path of '+ new_folder_path)
    '''define the first foloderNumber_index'''
    folderNumber_index = 0
    print('there are '+str(len(file_list))+' files at the path of '+path)
    for index,j in enumerate(file_list):
        #for i in range(0,len(file_list)):
        old_file_path = os.path.join(path,j)
        '''define the number,it decides how many imgs each folder process'''
        #number = 1000 #int(len(file_list)/folder_number)
        if(index%files_each_folder ==0 and index!=0):
            folderNumber_index +=1
        new_file_path = os.path.join(folderPath,'%s'%(folderNumber_index))
        if not os.path.exists(new_file_path):
            print('not exist path:'+new_file_path)
            break
        shutil.copy(old_file_path,new_file_path)
        print('success move file from '+ old_file_path +' to '+new_file_path)
    
    第 2 条附言  ·  2019-05-22 13:58:22 +08:00

    压缩 就加几行的事情

    for p in range(folder_number):
        exist_folder = os.path.join(folderPath,prefix_folder_back_Center+'%s'%p)
        shutil.make_archive(exist_folder,'zip',exist_folder)
        print('success make compress  of the '+ exist_folder )
    

    算是完成了这个小功能了。主要是方便自己偷懒,不用手去移动压缩了。

    5 条回复    2019-05-21 20:59:59 +08:00
    minami
        1
    minami  
       2019-05-21 16:56:29 +08:00
    获取文件名列表,然后用 tar 打包。其实 shell 脚本就能搞定
    chenqh
        2
    chenqh  
       2019-05-21 18:10:37 +08:00
    ```
    import os
    tmp_li = []
    for name in os.listdir(path):
    abs_path = os.path.join(path, name)
    tmp_li.append(abs_path)
    if len(tmp_li) == 10000:
    do_with(tmp_li)
    tmp_li = []
    if tmp_li:
    do_with(tmp_li)
    ```
    大概这个样子
    cxyfreedom
        3
    cxyfreedom  
       2019-05-21 18:31:13 +08:00
    有序列的话,不就是切片然后批量压缩吗?要快的话用多进程。shell 或者其他语言实现都是一样的
    thedrwu
        4
    thedrwu  
       2019-05-21 20:55:41 +08:00 via Android
    几行 shell 就解决了:

    a=`ls *.jpg` ; while [ -n "$a" ]; do zip `echo -e "$a" | head -1`.zip `echo -e "$a" | head -1000` ; a=`echo -e "$a" | sed -n '1001,$p' ` ; done

    选择合适的工具做合适的事。非要 Python 的话,os.system("""…""") 最合适了 (逃…
    mahonejolla
        5
    mahonejolla  
    OP
       2019-05-21 20:59:59 +08:00
    @thedrwu #4 哭泣,是时候在学一发 shell 了?逃…<<<------轮子哥语气。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5854 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 06:17 · PVG 14:17 · LAX 23:17 · JFK 02:17
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.