Pages

Friday, March 8, 2013

Recursively find list of files in a directory in Python 2.2

I have been using python for writing small utility programs. Till now I avoided scripting languages like python because I though shell script is master of everything and works in any linux computer. They also have all programming constructs like loops and conditions etc. However when it came to quickly get some work done, shell script too a lot of time write a error free program. Finally I jumped to python to do some file batch processing.

The most fundamental need to batch file processing is to iterate through all the files in a given directory. I could found several ways to do so using os.walk function but my old linux server had only Python 2.2 and I did not want to FTP files to my local PC to server back and fourth . So here is the code which also works in old version of python.

 
[code language="python"]
import os

# finds all the files in a directory recursively
def listAll(dirName):
filelist1=[]
files = os.listdir(dirName)
for f in files:
if os.path.isfile(os.path.join(dirName,f)):
filelist1.append(os.path.join(dirName,f))
else:
newlist=listAll(os.path.join(dirName,f));
filelist1.extend(newlist)
return filelist1
# root directory path
rootdir= '/home/'

for f in listAll(rootdir):
print "Absolute Path " + os.path.abspath(f)
print "File Name" + os.path.basename(f)
print "Directory Path" + os.path.dirname(f)
# Change directory
os.chdir(os.path.dirname(f))
# print current directory
os.system("pwd")
print os.getcwd()
# check file extension
if not ( f.endswith(".java") or or f.endswith(".xml")):
# Do nothing
continue


[/code]