首页 > 解决方案 > 更改图像名称 - PermissionError: [WinError 32] 该进程无法访问该文件,因为它正被另一个进程使用:

问题描述

尝试重命名文件夹中图像文件的名称时出现权限错误。代码是:

import PIL
import os

path = r"C:\Users\hjh\Pictures\Logging 16.04"
os.chdir(path)


for filename in os.listdir(path):
    if filename.endswith(".JPG"):
        #print(os.path.join(path, filename))
        imgname=filename
    
        image=PIL.Image.open(imgname)
    
        EXIF_data = image._getexif()
    
      
        datetime = EXIF_data.get(36867)
        datetime = datetime.replace(":","")
    
        os.rename(imgname,datetime)
    

        continue
    else:
        continue

有谁看到问题出在哪里?

谢谢!

标签: pythonpython-imaging-libraryrename

解决方案


你忘了添加文件路径。因此,操作系统会尝试将文件写入您运行 python 代码的位置。您应该使用以下内容:

datetime = EXIF_data.get(36867)
datetime = datetime.replace(":","")
os.rename(imgname,"{}.JPG".format(os.path.join(path , datetime)))

此外,在任何其他应用程序(例如图片查看器)上关闭您的文件


推荐阅读