Thursday, May 16, 2013

Python: listing all files and directories under a directory



import os

allfiles = list()

# listing all the files in the subdirectories
root = "yourDirectory/"
path = os.path.join(root, "targetdirectory")

for path, subdirs, files in os.walk(root):
    for name in files:
        s = os.path.join(path, name)
        allfiles.append(str(s))


for l in allfiles:
    print l

BZ2 compression & decompression in python

pretty easy!

let's say your .bz2 file is "filename":

import bz2

bfile = bz2.BZ2File(filename,"r")
for line in bfile:   
    print line.strip()