Date created and renamed Python file - criticism request

Scenario: when I photograph an object, I take several images from several angles. Multiplying the number of “shoot” objects, I can create a large number of images. Problem: The camera generates images identified as "DSCN100001", "DSCN100002", etc. Cryptic.

I put together a script that will offer a directory specification (Windows) as well as a "Prefix". The script reads the date and time the file was created and renames the file accordingly. The prefix will be added to the beginning of the file name. So, "DSCN100002.jpg" can become "FatMonkey 20110721 17:51:02". The chronology of time is important to me.

The following is a script. Please tell me if it is Pythonic, whether it is badly written or not, and of course, whether there is a cleaner - more efficient way to do this. Thanks.

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") 
+6
source share
3 answers

The way you do this looks Pythonic. Several alternatives (not necessarily suggestions):

You can skip os.chdir(target) and make os.path.join(target, filename) in a loop.

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

You can reuse the variable name, for example temp_date instead of t , v and x . It will be okay.

You can skip saving 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 can generalize your function for working with recursive directories with os.walk() .

You may find the file extension of the file so that it is correct not only for .jpeg s.

You can make sure that you only rename the form files DSCN1#####.jpeg

+7
source

Your code is nice and simple. Some possible improvements I can offer:

  • Command line arguments are preferred over dir names due to autocomplete with TAB
  • EXIF is a more accurate source of the date and time the photo was taken. If you change the photo in the image editor, modify time will be changed and EXIF ​​information will be saved. Here is a discussion of the EXIF ​​library for Python: Exif manipulation library for python
+3
source

My only thought is that if you are going to get the computer to do this work for you, let it do more work. My assumption is that you are going to shoot one object several times, then either move to another object or move another object to its place. If so, you might consider grouping photos by how close the timestamps are (possibly, any delta is considered a new object within 2 minutes). Then, based on these pseudo-clusters, you can name the photos by object.

It may not be what you are looking for, but thought that I would add to the proposal.

+2
source

Source: https://habr.com/ru/post/893288/


All Articles