1

I have directory with subdirectories and I have to make a list like:

file_name1 modification_date1 path1 
file_name2 modification_date2 path2 

and write the list into text file how can i do it in python?

James Hopkin
  • 13,797
  • 1
  • 42
  • 71
  • Duplicate: http://stackoverflow.com/questions/140758/looking-for-file-traversal-functions-in-python-that-are-like-javas, http://stackoverflow.com/questions/973473/getting-a-list-of-all-subdirectories-in-the-current-directory, http://stackoverflow.com/questions/775231/directory-walker-for-python – S.Lott Jun 30 '09 at 10:52
  • Duplicate: http://stackoverflow.com/questions/120656/directory-listing-in-python – S.Lott Jun 30 '09 at 10:53
  • Not an exact duplicate, although the links to the quasi-dups are informative. – Assaf Lavie Jun 30 '09 at 11:02
  • None of those links answer the modification time part of the question. – James Hopkin Jun 30 '09 at 11:02

2 Answers2

3

For traversing the subdirectories, use os.walk().

For getting modification date, use os.stat()

The modification time will be a timestamp counting seconds from epoch, there are various methods in the time module that help you convert those to something easier to use.

Lennart Regebro
  • 167,292
  • 41
  • 224
  • 251
3
import os
import time

for root, dirs, files in os.walk('your_root_directory'):
  for f in files:
    modification_time_seconds = os.stat(os.path.join(root, f)).st_mtime
    local_mod_time = time.localtime(modification_time_seconds)

    print '%s %s.%s.%s %s' % (f, local_mod_time.tm_mon, local_mod_time.tm_mday, local_mod_time.tm_year, root)  
James Hopkin
  • 13,797
  • 1
  • 42
  • 71