1

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?

Fredrik P
  • 682
  • 1
  • 8
  • 21

1 Answers1

0

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.

Aphex
  • 7,390
  • 5
  • 33
  • 54
  • Thanks! Not exactly what I was asking for, but likely what I _should_ have been asking for :) – Fredrik P Oct 25 '13 at 08:05
  • Yeah, I'm surprised fromFile doesn't release the file handle. Sounds like it's a bug in the library – Aphex Oct 25 '13 at 19:16
  • 1
    I patched pexif to correctly close the file handle and submitted a pull request here: https://github.com/bennoleslie/pexif/pull/1 - hope the author accepts it. – Aphex Oct 25 '13 at 19:33
  • It definitely is a bug in the pexif library. I've merged the pull request as above. Thanks Aphex. – benno Dec 15 '13 at 18:05