Created
November 23, 2011 19:27
-
-
Save yunzheng/1389638 to your computer and use it in GitHub Desktop.
File indexer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import os.path | |
import csv | |
import stat | |
import pwd | |
import grp | |
import sys | |
import datetime | |
ospathjoin = os.path.join | |
osstat = os.stat | |
def output_csv(rootdir): | |
for root, sub_folders, files in os.walk(rootdir): | |
try: | |
size = atime = mtime = ctime = 0 | |
for fname in files: | |
path = ospathjoin(root, fname) | |
if not os.path.isfile(path): | |
continue | |
fstat = osstat(path) | |
mode = oct(fstat[stat.ST_MODE])[-3:] | |
uid = fstat.st_uid | |
gid = fstat.st_gid | |
try: | |
user = pwd.getpwuid(uid)[0] | |
group = grp.getgrgid(gid)[0] | |
except: | |
user = uid | |
group = group | |
size = fstat.st_size | |
atime = datetime.datetime.fromtimestamp(int(fstat.st_atime)).strftime('%Y-%m-%d %H:%M:%S') | |
mtime = datetime.datetime.fromtimestamp(int(fstat.st_mtime)).strftime('%Y-%m-%d %H:%M:%S') | |
ctime = datetime.datetime.fromtimestamp(int(fstat.st_ctime)).strftime('%Y-%m-%d %H:%M:%S') | |
yield ctime, mtime, atime, mode, user, group, path | |
except Exception, e: | |
print >>sys.stderr, 'Error: %r, %s' % (path, e) | |
csv_writer = csv.writer(sys.stdout) | |
rootdir = '/' | |
for a in output_csv(rootdir): | |
csv_writer.writerow(a) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment