Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Walking through directories and files

Reply
Thread Tools

Walking through directories and files

 
 
Thierry Lam
Guest
Posts: n/a
 
      09-16-2005
I'm trying to use the os.walk() method to search all the directory from
a root directory and display their contents. For example, I want my
output to be like the following:


directoryA
stuffs.c
stuffs2.cpp

directoryB
asd.c
asdf.cpp


Any ideas how to do it? Currently, I can only print all the filenames
first and then the directory names.

Thanks
Thierry

 
Reply With Quote
 
 
 
 
Fredrik Lundh
Guest
Posts: n/a
 
      09-16-2005
Thierry Lam wrote:

> I'm trying to use the os.walk() method to search all the directory from
> a root directory and display their contents. For example, I want my
> output to be like the following:
>
>
> directoryA
> stuffs.c
> stuffs2.cpp
>
> directoryB
> asd.c
> asdf.cpp
>
> Any ideas how to do it?


import os

for dirpath, dirnames, filenames in os.walk("."):
print os.path.basename(dirpath)
for file in filenames:
print file
print

> Currently, I can only print all the filenames first and then the directory
> names.


why you cannot just put the print statements in the right order is more
than I can figure out, but I haven't had enough coffee today, so there's
probably a simple explanation.

</F>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Walking through Tate and Hibbs, running into things that don't work...startingon page 7 Xeno Campanoli Ruby 3 06-15-2007 06:12 PM
Re: Walking through a mysql db Jeff Elkins Python 2 06-04-2005 05:24 PM
Walking through a mysql db Jeff Elkins Python 2 06-04-2005 01:52 PM
Getting all directories/files from current directory and using -d flag for the directories Adam Petrie Perl Misc 8 10-11-2004 01:28 PM
walking through an array of char pointers gaga C Programming 19 08-23-2004 05:13 AM



Advertisments