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.