By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,996 Members | 1,498 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,996 IT Pros & Developers. It's quick & easy.

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

P: n/a
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()

Jun 27 '08 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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()
Jun 27 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.