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.n etwrote:
>From: Jeff McNeil <je**@jmcneil.n et> Subject: Re: Unable to write output from os.path.walk to a file. To: pd*****@yahoo.c om Cc: py*********@pyt hon.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 <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("Ente r directory to read:
")
>
listdir = os.path.walk (dirinput, visit, None)
f = open("walktxt", "w")
f.writelines(li stdir)
f.close()
-- http://mail.python.org/mailman/listinfo/python-list 0 2045 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 people can modifi the ini.
an example is the program mysql (meightbe someone has seen it)
that's exatly wath i'm trying to do.
|
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 in the os module got objectified
properly (or at least with some semblance of OO propriety!)
In the issues section:
|
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 using '/' as delimiters
and get the last 'word'. The reason I'm not entirely happy with that
method is that it is platform specific. I would prefer to use a built
in method if possible.
Cheers,
|
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 project in debug mode (by
hitting F5) it gives an error message
"Error while trying to run project: Unable to start debugging on the web
server.
|
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 procedures. It is enabled
for SQL stored procedures.
It is possible to "Build" and "Run" the Java SPs, it just isn't possible to
click on the "Build for Debug" option. Thanks for any help in advance.
Michael
| |
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 by the software in your host machine.
Stack Trace
at System.Net.Sockets.NetworkStream.Write(Byte buffer, Int32 offset, Int32
|
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 by the software in your host machine.
Stack Trace
at System.Net.Sockets.NetworkStream.Write(Byte buffer, Int32 offset, Int32
|
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):
print dirnames
|
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 in my example. Do you think that I have the arguments misplaced?
Thanks,
Paul
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |