Python File Creation Date & Rename - Request for Critique

橙三吉。 提交于 2019-12-22 05:41:47

问题


Scenario: When I photograph an object, I take multiple images, from several angles. Multiplied by the number of objects I "shoot", I can generate a large number of images. Problem: Camera generates images identified as, 'DSCN100001', 'DSCN100002", etc. Cryptic.

I put together a script that will prompt for directory specification (Windows), as well as a "Prefix". The script reads the file's creation date and time, and rename the file accordingly. The prefix will be added to the front of the file name. So, 'DSCN100002.jpg' can become "FatMonkey 20110721 17:51:02". The time detail is important to me for chronology.

The script follows. Please tell me whether it is Pythonic, whether or not it is poorly written and, of course, whether there is a cleaner - more efficient way of doing this. Thank you.

   import os
   import datetime
   target = raw_input('Enter full directory path: ')
   prefix = raw_input('Enter prefix: ')
   os.chdir(target)
   allfiles = os.listdir(target)
   for filename in allfiles:
        t = os.path.getmtime(filename)
        v = datetime.datetime.fromtimestamp(t)
        x = v.strftime('%Y%m%d-%H%M%S')
        os.rename(filename, prefix + x +".jpg")

回答1:


The way you're doing it looks Pythonic. A few alternatives (not necessarily suggestions):

You could skip os.chdir(target) and do os.path.join(target, filename) in the loop.

You could do strftime('{0}-%Y-%m-%d-%H:%M:%S.jpg'.format(prefix)) to avoid string concatenation. This is the only one I'd reccomend.

You could reuse a variable name like temp_date instead of t, v, and x. This would be OK.

You could skip storing temporary variables and just do:

for filename in os.listdir(target):
    os.rename(filename, datetime.fromtimestamp(
                         os.path.getmtime(filename)).strftime(
                          '{0}-%Y-%m-%d-%H:%M:%S.jpeg'.format(prefix)))

You could generalize your function to work for recursive directories by using os.walk().

You could detect the file extension of files so it would be correct not just for .jpegs.

You could make sure you only renamed files of the form DSCN1#####.jpeg




回答2:


Your code is nice and simple. Few possible improvements I can suggest:

  1. Command line arguments is more preferable for dir names because of autocomplition by TAB
  2. EXIF is more accurate source of date and time of photo creating. If you modify photo in image editor, modify time will be changed while EXIF information will be preserved. Here is discussion about EXIF library for Python: Exif manipulation library for python



回答3:


My only thought is that if you are going to have the computer do the work for you, let it do more of the work. My assumption is that you are going to shoot one object several times, then either move to another object or move another object into place. If so, you could consider grouping the photos by how close the timestamps are together (maybe any delta over 2 minutes is considered a new object). Then based on these pseudo clusters, you could name the photos by object.

May not be what you are looking for, but thought I'd add in the suggestion.



来源:https://stackoverflow.com/questions/6778923/python-file-creation-date-rename-request-for-critique

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!