By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,428 Members | 1,340 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,428 IT Pros & Developers. It's quick & easy.

Walking through directories and files

P: n/a
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

Sep 16 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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>

Sep 16 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.