现在是将文件等数量复制到每一个文件夹。暂时不会怎么压缩目录下的每一个文件夹。
# -*- 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)
压缩 就加几行的事情
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 )
算是完成了这个小功能了。主要是方便自己偷懒,不用手去移动压缩了。
1
minami 2019-05-21 16:56:29 +08:00
获取文件名列表,然后用 tar 打包。其实 shell 脚本就能搞定
|
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) ``` 大概这个样子 |
3
cxyfreedom 2019-05-21 18:31:13 +08:00
有序列的话,不就是切片然后批量压缩吗?要快的话用多进程。shell 或者其他语言实现都是一样的
|
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("""…""") 最合适了 (逃… |
5
mahonejolla OP @thedrwu #4 哭泣,是时候在学一发 shell 了?逃…<<<------轮子哥语气。
|