Created
August 31, 2015 16:39
-
-
Save chenzhan/7d3e1c9868caa63eddcb to your computer and use it in GitHub Desktop.
TraverseFiles
This file contains hidden or 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 sys | |
import sqlite3 as lite | |
def traverse(dir, cur): | |
ft = list(tuple()) | |
fl = os.listdir(dir) | |
for f in fl: | |
full_file_name = os.path.join(dir,f) | |
if os.path.isdir(full_file_name): | |
traverse(full_file_name, dir) | |
else: | |
path, filename = full_file_name.rsplit('/',1) | |
ft.append((path, filename, os.path.getsize(full_file_name))) | |
cur.executemany("INSERT INTO files VALUES(?, ?, ?)", ft) | |
# for path, subdirs, files in os.walk(ur'.'): | |
# for filename in files: | |
# ft.append( (path, filename, os.path.getsize(os.path.join(path,filename))) ) | |
con = None | |
try: | |
con = lite.connect('./files.db') | |
con.text_factory = str | |
cur = con.cursor() | |
traverse('.', cur) | |
con.commit() | |
except lite.Error, e: | |
print "Error: %s" % e.args[0] | |
sys.exit(1) | |
finally: | |
if con: | |
con.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment