python exifread在linux和mac上排序图像,在Windows上不起作用

时间:2019-06-11 06:59:59

标签: python

我练习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))

1 个答案:

答案 0 :(得分:1)

在Windows上,必须先关闭文件才能移动文件。或者,您将看到以上错误消息。

您必须在f.close命令之前进行move