user3203265
user3203265

Reputation: 11

Rename files based on their created time + date

I've just started learning Python and trying to understand what's wrong with the code below.

For the test purpose there are 50 images that I want to rename as Hour.Minute.Second_Year_Month_Day.jpg. The code below executes, but I'm getting the current time and date as file name, not the creation date of the images.

What am I missing? I was reading that getctime is for Windows and for Mac birthtime, or I'm talking nonsense (Bringing this up since I'm on a Mac) ?


directory = './'
extensions = (['.jpg', '.jpeg', '.png']);

filelist = os.listdir( directory )

newfilesDictionary = {}

count = 0

for file in filelist:
    filename, extension = os.path.splitext(file)
    if ( extension in extensions ):
        create_time = os.path.getctime( file )
        format_time = datetime.datetime.fromtimestamp( create_time )
        format_time_string = format_time.strftime("%H.%M.%S_%Y-%m-%d")
        newfile = format_time_string + extension; 

        if ( newfile in newfilesDictionary.keys() ):
            index = newfilesDictionary[newfile] + 1;
            newfilesDictionary[newfile] = index; 
            newfile = format_time_string + '-' + str(index) + extension;
        else:
            newfilesDictionary[newfile] = 0; 

        os.rename( file, newfile );
        count = count + 1
        print( file.rjust(35) + '    =>    ' + newfile.ljust(35) )


print( 'All done. ' + str(count) + ' files are renamed. ')

Upvotes: 1

Views: 2305

Answers (2)

Vasu Deo.S
Vasu Deo.S

Reputation: 1850

RECTIFIED CODE:-

import os
import datetime

directory = r'Dir_path'
extensions = (['.jpg', '.jpeg', '.png']);

filelist = os.listdir( directory )

newfilesDictionary = {}

count = 0

for file in filelist:
    filename, extension = os.path.splitext(file)
    if ( extension in extensions ):
        create_time = os.path.getctime( os.path.join(directory, file) )
        format_time = datetime.datetime.fromtimestamp( create_time )
        format_time_string = format_time.strftime("%H.%M.%S_%Y-%m-%d")
        newfile = format_time_string + extension;

        if ( newfile in newfilesDictionary.keys() ):
            index = newfilesDictionary[newfile] + 1;
            newfilesDictionary[newfile] = index;
            newfile = format_time_string + '-' + str(index) + extension;
        else:
            newfilesDictionary[newfile] = 0;

        os.rename( os.path.join(directory, file), os.path.join(directory, newfile))
        count = count + 1
        print( file.rjust(35) + '    =>    ' + newfile.ljust(35) )


print( 'All done. ' + str(count) + ' files are renamed. ')

Your code run's fine of Windows OS, just did a little improvement by adding os.path.join() to make it a lot more flexible while dealing with files. If you're on Mac, then try using os.stat(file).st_birthtime instead of os.path.getctime().

SUGGESTED IMPROVEMENTS:-

  • The Timestamp structure that you are using is not the best, as Timestamps are generally made in descending order of time duration i.e Year > Month > Day > Hour > Minutes > Seconds. This timestamp standard (ISO 8601) is commonly used.
  • Your should pass the extension of the file via lower() to make the extension all lowercase. As you code won't be able to deal with Image files with extension .JPG or .PNG.

Upvotes: 0

Basile
Basile

Reputation: 131

Under MacOS, you should try st_birthtime:

os.stat(file).st_birthtime

Note that your current code works as expected on Windows.

Upvotes: 1

Related Questions