Ebrahim Ghasemi
Ebrahim Ghasemi

Reputation: 6136

How to list latest modification time of contents of some directories?

Assume a structure like below:

People/
      Jimmy/
           my_Files/
                    work.txt
           report.txt
      Mohammad/
           the_work/
                    something2.sth
                    something2.sth

As you see, I have one directory for each clerk in the People directory and each clerk has a lot of files or directory in his/her directory.

I want to write a program to receive a date as input and search these directories and return which clerk hasn't modified any file after that date!

I'm using the following program to list all files:

files_path= list()
for root,dirs,files in os.walk(r"G:\People"):
    for filename in files:
        files_path.append(root+filename)

and the following program to receive the last modification time of them:

for single_file in files_path:
    print "last modified: %s" % time.ctime(os.path.getmtime(single_file))
    print "created: %s" % time.ctime(os.path.getctime(single_file))

Well, both works fine! The problem is I can't manage the result to have a readable output of pairs of user:last modification time!

Can anybody help me how handle it to have a rational output list?

Upvotes: 0

Views: 31

Answers (1)

Mike Müller
Mike Müller

Reputation: 85622

time.strftime gives you control over the formatting:

time.strftime('%H:%M:%S', time.gmtime(os.path.getmtime(single_file)))

A small helper function:

def format_time(seconds):
    return time.strftime('%H:%M:%S', time.gmtime(seconds))

Makes it more approachable:

print "last modified: %s, created: %s" %(format_time(os.path.getmtime(single_file)),
                                         format_time(os.path.getctime(single_file)))

Output:

last modified: 13:05:56, created: 13:05:56

Upvotes: 2

Related Questions