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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jeff McNeil

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

    Your args are fine, that's just the way os.path.walk works. If you
    just need the absolute pathname of a directory when given a relative
    path, you can always use os.path.abspath , too.

    A couple more examples that may help, using os.walk:
    >>for i in os.walk('/var/log'):
    .... for j in i[1] + i[2]:
    .... print os.path.join(i[0], j)
    ....
    /var/log/apache2
    /var/log/cups
    /var/log/fax
    /var/log/krb5kdc
    /var/log/ppp
    /var/log/sa
    /var/log/samba

    Or, in the event that a relative path was used:
    >>for i in os.walk(os.path .abspath('../../var/log')):
    .... for j in i[1] + i[2]:
    .... print os.path.join(i[0], j)
    ....
    /var/log/apache2
    /var/log/cups
    /var/log/fax
    /var/log/krb5kdc
    /var/log/ppp
    /var/log/sa
    /var/log/samba






    On Wed, Jun 4, 2008 at 6:07 PM, Paul Lemelle <pdl5000@yahoo. comwrote:
    Jeff,
    >
    Thanks for your reply. I would like to like the absolute path of a directory. I thought that os.listdir just returns the nam itself in a data list.
    >
    I noticed that none was being return in my example. Do you think that I have the arguments misplaced?
    >
    Thanks,
    Paul
    >
    >
    >
    --- On Wed, 6/4/08, Jeff McNeil <jeff@jmcneil.n etwrote:
    >
    >From: Jeff McNeil <jeff@jmcneil.n et>
    >Subject: Re: Unable to write output from os.path.walk to a file.
    >To: pdl5000@yahoo.c om
    >Cc: python-list@python.org
    >Date: Wednesday, June 4, 2008, 3:26 PM
    >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(dir input) 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@yahoo .comwrote:
    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("Ente r directory to read:
    >")
    >
    listdir = os.path.walk (dirinput, visit, None)
    >
    f = open("walktxt", "w")
    >
    f.writelines(li stdir)
    >
    f.close()
    >
    >
    >
    >
    >
    --

    >
    >
    >
    >
    >
Working...