Unable to write output from os.path.walk to a file.

Larry Bates larry.bates at websafe.com`
Wed Jun 4 15:22:21 EDT 2008


Paul Lemelle wrote:
> I Am trying to output the os.path.walk to a file, but the writelines method complains....
> 
> Below is the code, any helpful suggestions would be appreciated. 
> 
> def visit(arg, dirnames, names):
> 	print dirnames
> 	
> 
> 
> 
> dirinput = raw_input("Enter directory to read: ")
> 
> listdir = os.path.walk (dirinput, visit, None)
> 
> f = open("walktxt", "w")
> 
> f.writelines(listdir)
> 
> f.close()
> 
>  
> 
> 
>       
os.path.walk doesn't return a list of names.  I think what you want is something 
like:

import os
def visit(f, dirname, names):
     f.write(dirname+'\n')


f=open('c:\\walktxt', 'w')
dirinput = raw_input("Enter directory to read: ")
os.path.walk(dirinput, visit, f)
f.close()



More information about the Python-list mailing list