0

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)

Before: enter image description here

After: enter image description here

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.

Alex Günsberg
  • 118
  • 1
  • 9
  • You could use `sorted` with `enumerate(sorted(os.listdir())):` – Guillaume Jacquenot May 15 '23 at 13:53
  • 2
    Check out https://stackoverflow.com/questions/168409/how-do-you-get-a-directory-listing-sorted-by-creation-date-in-python – Elijah Sink May 15 '23 at 13:53
  • Does this answer your question? [How do you get a directory listing sorted by creation date in python?](https://stackoverflow.com/questions/168409/how-do-you-get-a-directory-listing-sorted-by-creation-date-in-python) – 9769953 May 15 '23 at 13:54
  • 1
    As an aside, you want to use machine-readable stable numbering in the result. Try `"tumor%05i" % count` – tripleee May 15 '23 at 14:00
  • It somehow erased most of the files altogether! I need to re-download it and see if it works. I was too stupid to make a backup before testing. – Alex Günsberg May 15 '23 at 14:00
  • Still numers them in a random order for some reason – Alex Günsberg May 15 '23 at 14:05

2 Answers2

1

As was suggested in the comments and this answer, you need to use sorted() and the key by which you want to sort.

By default, if given a list of strings as input, sorted() returns the alphabetically sorted list. If you want to use a different sorting criterion, you must specify it. In this case, you are looking for the functions os.path.getctime (for the creation time) and os.path.getmtime (for the modification time).

Following code will print the contents of your directory as desired.

import os

print("directory contents listed alphabetically : ")
for count, f in enumerate(os.listdir()):
    print(f"{count:03} : {f}")
print()

print("directory contents listed based on creation time : ")
for count, f in enumerate(sorted(os.listdir(), key=os.path.getctime)):
    print(f"{count:03} : {f}")
print()

# list based on modification time
print("directory contents listed based on modification time : ")
for count, f in enumerate(sorted(os.listdir(), key=os.path.getmtime)):
    print(f"{count:03} : {f}")
print()
Hoodlum
  • 950
  • 2
  • 13
0

Try this:

import os

folder_path = '/path/to/folder' 
files = os.listdir(folder_path)
sorted_files = sorted(files, key=lambda x: os.path.getmtime(os.path.join(folder_path, x)))
for file_name in sorted_files:
    print(file_name)
Carmelot
  • 46
  • 2