문제

I'm using the PEXIF module to read and edit EXIF data in JPEG files. After reading a file's data I would like to rename the file, but by then it is locked and os.rename() throws a WindowsError.

import pexif, os
f = 'oldName.jpg'
img = pexif.JpegFile.fromFile(f)
print img.exif.primary.ExtendedEXIF.DateTimeOriginal
os.rename(f, 'newName.jpg')

How can I unlock the file?

도움이 되었습니까?

해결책

Why not use fromFd instead:

f = 'oldName.jpg'
with open(f, "rb") as fd:
    img = pexif.JpegFile.fromFd(fd)
print img.exif.primary.ExtendedEXIF.DateTimeOriginal
os.rename(f, 'newName.jpg')

The file handle will be closed when the with block's scope ends, so the rename will work.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top