473,287 Members | 1,580 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,287 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 1993

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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.