Re: [问题] 多档案分多个资料夹

楼主: kenduest (小州)   2022-10-07 18:32:21
※ 引述《shihyu (shihyu)》之铭言:
: 我有1万张图片大约有5G容量, 想把这1万张图每2G分成一个资料夹
: 请问一下有什么linux 指令可以做到?
: 谢谢
#!/bin/env python3
from pathlib import Path
import os
import shutil
number=0
current_size=0
pattern = "new-folder-%03d"
new_directory = pattern % number
Path(pattern % number).mkdir(parents=True, exist_ok=True)
# 默认抓 /path/picture/* 该目录内档案,若是要包含子目录,请使用像是 rglob('*.jpg') 替代
for item in Path('/path/picture/').glob('*'):
current_size+=item.stat().st_size
if current_size >= 1024 * 1024 * 3:
number+=1
current_size = 0
new_directory = pattern % number
Path(new_directory).mkdir(parents=True, exist_ok=True)
shutil.move(os.path.join(item.parent, item.name), os.path.join(new_directory, item.name))
直觉写的 code,用 python 语言。提供参考。里面路径与档案大小可以自行调整。
作者: relaughter (好好笑)   2022-10-07 20:57:00
大大太佛了,推一下
作者: ases60909   2022-10-08 22:42:00
用python就是方便

Links booklink

Contact Us: admin [ a t ] ucptt.com