473,779 Members | 2,001 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

A little assistance with os.walk please.

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, files in os.walk('/directory/'):
print root
# print dirs
# print files

Aug 14 '06 #1
7 4565
On Mon, 14 Aug 2006 07:44:39 -0700, KraftDiner wrote:
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, files in os.walk('/directory/'):
print root
# print dirs
# print files
>>print os.walk.__doc__
Directory tree generator.

For each directory in the directory tree rooted at top (including top
itself, but excluding '.' and '..'), yields a 3-tuple

dirpath, dirnames, filenames

dirpath is a string, the path to the directory. dirnames is a list of
the names of the subdirectories in dirpath (excluding '.' and '..').
filenames is a list of the names of the non-directory files in dirpath.
Note that the names in the lists are just names, with no path components.
To get a full path (which begins with top) to a file or directory in
dirpath, do os.path.join(di rpath, name).

If optional arg 'topdown' is true or not specified, the triple for a
directory is generated before the triples for any of its subdirectories
(directories are generated top down). If topdown is false, the triple
for a directory is generated after the triples for all of its
subdirectories (directories are generated bottom up).

When topdown is true, the caller can modify the dirnames list in-place
(e.g., via del or slice assignment), and walk will only recurse into the
subdirectories whose names remain in dirnames; this can be used to prune
the search, or to impose a specific order of visiting. Modifying
dirnames when topdown is false is ineffective, since the directories in
dirnames have already been generated by the time dirnames itself is
generated.

By default errors from the os.listdir() call are ignored. If
optional arg 'onerror' is specified, it should be a function; it
will be called with one argument, an os.error instance. It can
report the error to continue with the walk, or raise the exception
to abort the walk. Note that the filename is available as the
filename attribute of the exception object.

Caution: if you pass a relative pathname for top, don't change the
current working directory between resumptions of walk. walk never
changes the current directory, and assumes that the client doesn't
either.

Example:

from os.path import join, getsize
for root, dirs, files in walk('python/Lib/email'):
print root, "consumes",
print sum([getsize(join(ro ot, name)) for name in files]),
print "bytes in", len(files), "non-directory files"
if 'CVS' in dirs:
dirs.remove('CV S') # don't visit CVS directories
Aug 14 '06 #2
The os.walk function walks the operating systems directory tree.

Yup.
This seems to work, but I don't quite understand the tupple that is
returned...
Can someone explain please?

for root, dirs, files in os.walk('/directory/'):
print root
# print dirs
# print files

As cheesy as it may sound, try uncommenting the two commented
lines. For a more explicit variant:

print repr(dirs)
print repr(files)

You'll notice that they're lists.

In the directory "root", you'll find the subdirectories given in
the list "dirs" and you'll find the files given in the list "files".

-tkc

Aug 14 '06 #3
KraftDiner wrote:
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, files in os.walk('/directory/'):
print root
# print dirs
# print files
Actually returns two tuples: dirs and files

root - is the directory branch you are currently walking
dirs - are the directory branches that are subdirectories of this
directory branch
files - are the files that live in this directory branch
To process all the files here you do something like:

for afile in files: # resist the urge to call it 'file'
fullpath=os.pat h.join(root, afile)
#
# Do something with fullpath
#

Hard to figure out item - If you wish to NOT process some of the
dirs, you can delete them from the dirs list here and they won't
get walked. You MUST delete them in place with del dirs[n] or
dirs.pop or some other function that deletes in-place.

You might want to type: help(os.walk) to get some more info.

-Larry Bates
Aug 14 '06 #4

Larry Bates wrote:
KraftDiner wrote:
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, files in os.walk('/directory/'):
print root
# print dirs
# print files

Actually returns two tuples: dirs and files

root - is the directory branch you are currently walking
dirs - are the directory branches that are subdirectories of this
directory branch
files - are the files that live in this directory branch
To process all the files here you do something like:

for afile in files: # resist the urge to call it 'file'
fullpath=os.pat h.join(root, afile)
#
# Do something with fullpath
#

Hard to figure out item - If you wish to NOT process some of the
dirs, you can delete them from the dirs list here and they won't
get walked. You MUST delete them in place with del dirs[n] or
dirs.pop or some other function that deletes in-place.

You might want to type: help(os.walk) to get some more info.
Yep done that. Thanks.
Two things..
1) there seems to be an optional topdown flag. Is that passed to
os.walk(path, topdownFlag)
2) I only want to process files that match *.txt for example... Does
that mean I need to parse the list of files for the .txt extention or
can I pass a wildcard in the path parameter?

-Larry Bates
Aug 14 '06 #5
1) there seems to be an optional topdown flag. Is that passed to
os.walk(path, topdownFlag)
Yes.
2) I only want to process files that match *.txt for example... Does
that mean I need to parse the list of files for the .txt extention or
can I pass a wildcard in the path parameter?
>>for path, dirs, files in os.walk("."):
.... for f in files:
.... if not f.lower().endsw ith(".txt"): continue
.... print os.path.join(pa th, f)

If you want to be more complex:

>>from os.path import splitext
allowed = ['.txt', '.sql']
for path, dirs, files in os.walk("."):
.... for f in files:
.... if splitext(f)[1].lower() not in allowed: continue
.... fn = os.path.join(pa th, f)
.... print "do something with %s" % fn
Just a few ideas,

-tkc


Aug 14 '06 #6

Tim Chase wrote:
1) there seems to be an optional topdown flag. Is that passed to
os.walk(path, topdownFlag)

Yes.
2) I only want to process files that match *.txt for example... Does
that mean I need to parse the list of files for the .txt extention or
can I pass a wildcard in the path parameter?
>>for path, dirs, files in os.walk("."):
... for f in files:
... if not f.lower().endsw ith(".txt"): continue
... print os.path.join(pa th, f)

If you want to be more complex:

>>from os.path import splitext
>>allowed = ['.txt', '.sql']
>>for path, dirs, files in os.walk("."):
... for f in files:
... if splitext(f)[1].lower() not in allowed: continue
... fn = os.path.join(pa th, f)
... print "do something with %s" % fn
Just a few ideas,

-tkc

Many thanks all.
B.

Aug 14 '06 #7
>>allowed = ['.txt', '.sql']
>>for path, dirs, files in os.walk("."):
... for f in files:
... if splitext(f)[1].lower() not in allowed: continue
Additionally, if you really do want to specify wildcards:
>>allowed = ['*.txt', 'README*', '*.xml', '*.htm*']
from glob import fnmatch
import os
for path, dirs, files in os.walk("."):
.... good_files = []
.... for pat in allowed:
.... good_files.exte nd(fnmatch.filt er(files, pat))
.... if good_files: break
.... for f in good_files:
.... print "doing something with %s" %
os.path.join(pa th, f)

-tkc



Aug 14 '06 #8

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

Similar topics

9
9368
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 structure, no? Also,it generates the below error during the os.renames section, but the odd thing is that it actually renames the files before saying it can't find them. Any ideas are welcomed. If I'm doing something *really* wrong here, just let me...
9
2915
by: Marcello Pietrobon | last post by:
Hello, I am using Pyton 2.3 I desire to walk a directory without recursion this only partly works: def walk_files() : for root, dirs, files in os.walk(top, topdown=True): for filename in files: print( "file:" + os.path.join(root, filename) ) for dirname in dirs:
6
4694
by: rbt | last post by:
More of an OS question than a Python question, but it is Python related so here goes: When I do os.walk('/') on a Linux computer, the entire file system is walked. On windows, however, I can only walk one drive at a time (C:\, D:\, etc.). Is there a way to make os.walk() behave on Windows as it behaves on Linux? I'd like to walk the entire file system at once... not one drive at a time. Thanks!
1
5988
by: sumanthsclsdc | last post by:
hello friends, i am using the os.path.walk to search to filter the files with the given word, this function i m using in many modules. i just want to place this function in some class and call it whenever required by passing the necessary word. for e.g i want to filter files with "Spec" which is specification documents and store in some list, and other time with word "doc" like this many times...so i want to place it in some...
9
2876
by: silverburgh.meryl | last post by:
i am trying to use python to walk thru each subdirectory from a top directory. Here is my script: savedPagesDirectory = "/home/meryl/saved_pages/data" dir=open(savedPagesDirectory, 'r') for file in dir: if (isdir(file)): # get the full path of the file
2
4021
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", len(files), "non-directory files" if 'CVS' in dirs: dirs.remove('CVS') # don't visit CVS directories
2
1829
by: | last post by:
Hi everyone The following code: scriptPath = os.path.dirname(__file__) (dirpath, dirnames, filenames) = os.walk(scriptPath) print 'dirpath\n' print dirpath print 'dirnames\n' pprint.pprint(dirnames)
0
2039
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 more examples that may help, using os.walk: .... for j in i + i: .... print os.path.join(i, j) .... /var/log/apache2
3
2260
by: gaurav92K | last post by:
sir i am working in a company . there are many pc. i want to use remote assistance. i configure all group policy which are related remote assistance.and i enable service through remote in system property.every services related remote desktop & remote assistance are start . but i can not use help assistance through another system while i try help assistance in my pc then help assistance begin start. what is problem please give the best answer on...
0
9632
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9471
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,...
0
10136
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 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...
1
10071
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9925
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 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...
1
7478
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6723
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();...
0
5372
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3631
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.