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]

5 comments:

  1. ...Check this out

    [...]Wow, awesome blog layout! How long have you been blogging for?[...]

    ReplyDelete
  2. ...Links

    [...] What web host are you the usage of? Can I get affiliate hyperlink to your host? I wish web site loaded up as quickly as yours lol[...]

    ReplyDelete
  3. ...Visitor recommendations

    [...]Wow, wonderful blog layout! How long have you been blogging for?[...]

    ReplyDelete
  4. Hello, how's it going? Just shared this post with a colleague, we had a good laugh.
    Cheap Louis Vuitton planner http://www.indianpod.com/louisvuitton/Cheap-Louis-Vuitton-planner.html

    ReplyDelete
  5. Hello there, I found your blog by the use of Google at the same time as searching for a related topic, your web site came up, it seems great. I have bookmarked it in my google bookmarks.
    Cheap Louis Vuitton mini lin http://www.indianpod.com/louisvuitton/Cheap-Louis-Vuitton-mini-lin.html

    ReplyDelete