I'm using this code to rename all files in a directory.
# Python program to rename all file
# names in your directory
import os, time
os.chdir(r'/Users/al/Library/Mobile Documents/com~apple~CloudDocs/data/mental/tumor/2017')
print(os.getcwd())
for count, f in enumerate(os.listdir()):
f_name, f_ext = os.path.splitext(f)
f_name = "tumor" + str(count)
new_name = f'{f_name}{f_ext}'
os.rename(f, new_name)
The code enumerates them in some random order. I would like it to enumerate the files based on the creation or modification date. Is it possible? They include monthly data for a given year, and I have tens of similar folders, so re-sorting them manually is a pain.
Using enumerate(sorted(os.listdir())):
does not change the behavior.