Problem opening files while using os.path.walk
R. Arens
martian_bob at portalofevil.com
Thu Nov 28 16:37:47 EST 2002
Hi. I'm writing what should be a simple function that will walk a
directory structure, opening and joining all files within the
directory tree into a single huge file. I'm using os.path.walk and
testing the list of files returned to the function specified in walk
to make sure that each file is actually a file as opposed to another
directory - herein lies the problem. The function isn't seeing
_anything_ as a file, and is just skipping it. This one has me quite
perplexed, and I'd appreciate any help anyone can offer. Thanks!
Code follows....
import os, sys
def walkJoin(out, dirname, names):
print "Current directory: " + dirname
names.sort()
for name in names:
if os.path.isfile(name):
file = open(name, 'r')
lines = file.readlines()
for line in lines:
out.writeline(line)
file.close()
else:
print "Keep walking"
os.path.walk(name, walkJoin, out)
print "Done with directory ", name
def main():
out = open('out.txt', 'w')
directory = raw_input("Enter the name of the directory: ")
print "Root directory " + directory
os.path.walk(directory, walkJoin, out)
out.close()
More information about the Python-list
mailing list