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

Jeff McNeil jeff at jmcneil.net
Wed Jun 4 16:26:23 EDT 2008


What exactly are you trying to accomplish? If you're just looking for
the contents of a directory, it would be much easier to simply call
os.listdir(dirinput) as that will return a list of strings that
represent the entries in dirinput.

As it stands, 'os.path.walk' will return None in your example, thus
the reason f.writelines is failing, the error says something about a
required iterable, no?

You ought to look at os.walk anyways, as I believe it is the preferred
approach when walking a directory hierarchy. It's a generator that
will yield a tuple that contains (dirname, subdirectories, filenames).
It seems that is what you're looking for?

Thanks,

Jeff




On Wed, Jun 4, 2008 at 2:54 PM, Paul Lemelle <pdl5000 at yahoo.com> 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()
>
>
>
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list