Python File Creation Date & Rename - Request for Critique

心不动则不痛 提交于 2019-12-05 08:06:56

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

Kirill

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

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.

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