import os
import shutil
files = os.listdir('.')
for f in files:
# skip non-jpeg files
if not f.endswith('.jpg'):
continue
# move if panchromatic
if '2302' in f or '3211' in f:
shutil.move(f, os.path.join('panchromatic', f))
# move if sepia
elif '7603' in f:
shutil.move(f, os.path.join('sepia', f))
# notify if something else
else:
print('Could not categorize file with name %s' % f)
自我解释,使用Python 3或Python 2+backport
pathlib
:在不给你解决方案的情况下,我建议你这样做。在
使用
os.listdir
遍历目录中的文件。在将字符串切片检查4位数字。从外观上看,你需要得到
file[6:10]
选中
if int(file[6:10]) in {2302, 2311}
。如果是,dst = /path/to/panchromatic
。{/Else}使用}连接路径人工制品。
shutil.move
移动文件。类似于shutil.move(os.path.join(path, file), dst)
,其中{确保}位于脚本顶部。
import os
和{你可以用简单的方法,也可以用难的方法。在
简单的方法
测试文件名是否包含要查找的子字符串。在
当前形式的这个解决方案很容易被错误分类,因为我们要查找的数字可能会偶然出现在字符串中。我会让你去想办法缓解这种情况。在
艰难的道路
正则表达式。将日期后的四个字母数字与正则表达式匹配。留给你去探索!在
相关问题 更多 >
编程相关推荐