I am trying to write a script where it traverses the current folder and all subfolders and files and prints to screen the name of the files, filesize, and date last modified.
e.g.
test0.xml | 26 bytes | 11/21/08 23:35:28 (I can get it to do this)
Folder/test1.xml | 26 bytes | 11/21/08 23:35:28 (this I can't)
Folder/folder2/test2.xml | 26 bytes | 11/21/08 23:35:28
I can easily get it to work for all the folders in the current directory but always run into problems in python when I try to use all sub directories also..
#!/usr/python/bin
# Filename: log_file.py
"""
This file takes a file in a directory and writes a log file containing all the filenames, modification times and filesizes. Then on a later date, the program will check this log file to detect any changes in the directory
"""
import os, glob, time
cwd = os.getcwd()
log = open('log_file.txt', 'w')
for dirpath,dirs,files in os.walk(cwd):
for dirs in os.walk(cwd):
for f in dirs:
# psplit = os.path.split(f)[1]
for file in files:
size = os.stat(file)
file_date = time.strftime("%m/%d/%y %H:%M:%S", time.localtime())
print file,'|','%s' % size.st_size,'','bytes','|','%s' % file_date,''
Could someone explain what I need to do please.