python - 更改图像名称 - 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
有谁看到问题出在哪里?
谢谢!
解决方案
你忘了添加文件路径。因此,操作系统会尝试将文件写入您运行 python 代码的位置。您应该使用以下内容:
datetime = EXIF_data.get(36867)
datetime = datetime.replace(":","")
os.rename(imgname,"{}.JPG".format(os.path.join(path , datetime)))
此外,在任何其他应用程序(例如图片查看器)上关闭您的文件
推荐阅读
- javascript - javascript - 为什么我不能用 React 更新我的表单字段
- c++ - 如何计算生成几何的每个顶点法线?OpenGL
- discord.js - 如何将文件附加到消息中?
- react-native - 如何按组显示日期,例如 Whatsapp 聊天屏幕?
- angular - ENOENT:没有这样的文件或目录,打开'/home/vsts/work/package.json'
- ios - UICollectionView relodItemsAtIndexPaths 重新加载集合视图的所有项目
- c++ - 如何从变量类型中删除`constexpr`
- mysql - 将虚拟数据添加到 laravel 中的“连接表”中的最佳方法是什么?
- python - 在 Django 中自定义 models.py 时,我应该设置什么作为默认参数?
- css - 在 shadow dom 中使用 CSS 选择器,例如 :first-child