473,396 Members | 1,996 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,396 software developers and data experts.

os.walk trouble

As my first Python script, I am trying to make a program that recurses
a directory searching for files whose names match a pattern. I have a
basic idea of what the regexp would look like (and I can get more
information about that), but I am stuck with incorrect understanding of
os.walk. I've tried:

root, dirs, files = os.walk(dirname)

but it fails for some reason. I have seen this done before in the
tutorial (I think it's called sequence unpacking). What is the proper
way to do this?

Thanks in advance.

Jun 1 '06 #1
6 1911
> but I am stuck with incorrect understanding of
os.walk. I've tried:

root, dirs, files = os.walk(dirname)


os.walk returns an iteratable sequence of those tuples. Thus,
you want to have

for filepath, dirs, files in os.walk(dirname):
#you're looking at the "dirs" and "files" in filepath
print "Currently in %s" % filepath
print "\t[Directories in %s]" % filepath
print "\n\t".join(dirs)
print "\t[Files in %s]" % filepath
print "\n\t".join(files)
print "=" * 50

HTH,

-tkc


Jun 1 '06 #2
Neat. And looks better, at least on my machine, if you had a tab or two
and an extra \n after the dirs.

rick

for filepath, dirs, files in os.walk(root):
#you're looking at the "dirs" and "files" in filepath
print "Currently in %s" % filepath
print "\t[Directories in %s]" % filepath
print '\t' + "\n\t".join(dirs) + '\n'
print "\t[Files in %s]" % filepath
print '\t' + "\n\t".join(files)
print "=" * 50

Jun 1 '06 #3
>> root, dirs, files = os.walk(dirname)

If you want to do it this way, you need to stop it after the first one:

root, dirs, files = os.walk(dirname).next()
print root
print dirs
print files

see this thread

http://tinyurl.com/rmyo4

Jun 1 '06 #4
> As my first Python script, I am trying to make a program that recurses
a directory searching for files whose names match a pattern. I have a
basic idea of what the regexp would look like


You probably don't need regexp for this, just use the fnmatch module

http://docs.python.org/lib/module-fnmatch.html

There's a great recipe in the Python Cookbook 2ed for just what you're
trying to do. In fact, I believe there's a recipe in there for just
about anything anybody (at the beginner or intermediate level) wants to
do with Python.

The recipe, with some other unrelated problems:

http://tinyurl.com/nvmzg

Amazon Python Cookbook link:

http://www.amazon.com/exec/obidos/as...973/inscape-20

good luck

rick

Jun 1 '06 #5
The Prophet wrote:
As my first Python script, I am trying to make a program that recurses
a directory searching for files whose names match a pattern.


If your patterns are simple (supported by fnmatch), the path module
makes this very easy:
import path
for f in path.path(dirname).walkfiles('*.foo'):
# process a .foo file here

http://www.jorendorff.com/articles/p...ath/index.html

Kent
Jun 1 '06 #6

Kent Johnson wrote:
The Prophet wrote:
As my first Python script, I am trying to make a program that recurses
a directory searching for files whose names match a pattern.


If your patterns are simple (supported by fnmatch), the path module
makes this very easy:
import path
for f in path.path(dirname).walkfiles('*.foo'):
# process a .foo file here

http://www.jorendorff.com/articles/p...ath/index.html

Kent


Well, it ain't that simple, but the suggestion is helpful, since I am
only a beginner. Thanks for everything, friends, now this aspect is
clear to me.

Jun 1 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: RC | last post by:
I assume the answer is yes. I try to write readSubdir function read all files in all sub-directories. <?php readSubdir("Documents/2005"); function readSubdir($dirPath) { $dirPointer =...
9
by: hokieghal99 | last post by:
This script is not recursive... in order to make it recursive, I have to call it several times (my kludge... hey, it works). I thought os.walk's sole purpose was to recursively walk a directory...
8
by: deko | last post by:
I'm hoping someone can sanity check my understanding of the Object Model for Forms/Controls. I'm having trouble drilling down into Control properties. First, I have a record set with the...
2
by: SKG | last post by:
Hi All I am using standard code to walk through a multi select list boxes and build a sql string to filter data, I would also like to store the 2nd column value for each selected item as well in...
7
by: KraftDiner | last post by:
The os.walk function walks the operating systems directory tree. This seems to work, but I don't quite understand the tupple that is returned... Can someone explain please? for root, dirs,...
8
by: Andre Meyer | last post by:
Hi all os.walk() is a nice generator for performing actions on all files in a directory and subdirectories. However, how can one use os.walk() for walking through two hierarchies at once? I want...
2
by: gregpinero | last post by:
In the example from help(os.walk) it lists this: from os.path import join, getsize for root, dirs, files in walk('python/Lib/email'): print root, "consumes", print sum(), print "bytes in",...
0
by: Jeff McNeil | last post by:
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...
4
by: Jeff Nyman | last post by:
Greetings all. I did some searching on this but I can't seem to find a specific solution. I have code like this: ========================================= def walker1(arg, dirname, names):...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...
0
agi2029
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,...

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.