我练习 python 并编写脚本,根据创建日期对我的照片进行排序,这是由 exifread 包完成的。我在 mac os 和 linux 上对其进行了测试,它完美无缺,但是在 Windows 上我首先遇到错误,即由于分号无法命名目录,例如“2016:06”,因此我将其更改为“2016-06” ,但脚本仍然显示如下错误:
Traceback (most recent call last):
File "imageSorter.py", line 25, in <module>
sortPhotos()
File "imageSorter.py", line 22, in sortPhotos
shutil.move((input_dir + name), (output_dir + name))
File "C:\Python27\lib\shutil.py", line 326 in move
os.unlink(src)
WindowsError: [Error 32] The process cannot used by another process: './landscape.jpg'
我的代码是这样的:
def sortPhotos():
input_dir = './'
for name in os.listdir(input_dir):
if name.endswith('.jpg'):
f = open(name, 'rb')
tags = exifread.process_file(f)
for tag in tags.keys():
if tag in ('Image DateTime'):
checked_dir_year = str(tags[tag])[0:4]
checked_dir_month = str(tags[tag])[5:7]
checked_dir_no_semicolor = checked_dir_year + '-' + checked_dir_month
checked_dir = str(tags[tag])[0:7]
if not os.path.exists(checked_dir_no_semicolor):
os.mkdir(checked_dir_no_semicolor)
if checked_dir in str(tags[tag]):
output_dir = './' + checked_dir_no_semicolor + '/'
shutil.move((input_dir + name), (output_dir + name))
在 Windows 上,移动文件之前,您必须关闭它。或者,您将看到上述错误消息。
你必须:f.close
在move
命令之前
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句