473,472 Members | 2,148 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 <pd*****@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 <je**@jmcneil.netwrote:
>From: Jeff McNeil <je**@jmcneil.net>
Subject: Re: Unable to write output from os.path.walk to a file.
To: pd*****@yahoo.com
Cc: py*********@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(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
<pd*****@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("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

Jun 27 '08 #1
0 2011

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Gurk | last post by:
hi the settings of my program are stored in an ini file, is there a simple way to open the file in some textbox, ore a betteer soutable box. i also want it to be able to resave the ini so...
31
by: John Roth | last post by:
I'm adding a thread for comments on Gerrit Holl's pre-pep, which can be found here: http://tinyurl.com/2578q Frankly, I like the idea. It's about time that all of the file and directory stuff...
2
by: Rob Cowie | last post by:
Hi, Given a string representing the path to a file, what is the best way to get at the filename? Does the OS module provide a function to parse the path? or is it acceptable to split the string...
16
by: Serdar Kalaycý | last post by:
Hi everybody, My problem seems a bit clichè but I could not work around. Well I read lots of MSDN papers and discussions, but my problem is a bit different from them. When I tried to run the...
2
by: Michael | last post by:
Running DB2 v7 UDB ("DB2 v7.1.0.93", "n031208" and "WR21333") on Windows XP, I am unable to find out why the "Build for Debug" option within Stored Procedure Builder is not enabled on Java stored...
0
by: Buddy Home | last post by:
Hello, I'm trying to upload a file programatically and occasionally I get the following error message. Unable to write data to the transport connection: An established connection was aborted...
3
by: Buddy Home | last post by:
Hello, I'm trying to upload a file programatically and occasionally I get the following error message. Unable to write data to the transport connection: An established connection was aborted...
1
by: Paul Lemelle | last post by:
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):...
0
by: Paul Lemelle | last post by:
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.