473,394 Members | 1,773 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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 2004

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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.