user14380579
user14380579

Reputation:

Determine last modified date of folders by scanning their subfolders

I have a folder containing many subfolders.

I'm trying to write a function that scans the entire folder and returns the name of each subfolder along with the last modified date.

import os,time

def get_information(directory):
    file_list = []
    for i in os.listdir(directory):
        a = os.stat(os.path.join(directory,i))
        file_list.append([i, time.ctime(a.st_atime)])
    return file_list

The above works, but I actually want it to to consider all subfolders and files when determining the last modified date.

For example when I look into the folder I'm scanning, I can see one folder called '20050'. The last modified date is 2021/09/09. However, when I go into that folder, I can see files and folders in there which were modified in 2022/01/01.

Not sure why this is, and struggling to write any code that'll scan these subfolders to determine last modified date.

Upvotes: 0

Views: 619

Answers (1)

Harsha Biyani
Harsha Biyani

Reputation: 7268

You can try os.walkgenerates the file names in a directory tree by walking the tree:

def get_information(directory):
    file_list = []
    for (root, dirs, files) in os.walk(directory):
        for file in files:
            a = os.stat(os.path.join(root, file))
            file_list.append([file, time.ctime(a.st_atime)])
    return file_list

print(get_information("C:"))

Upvotes: 1

Related Questions