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. 6 1918
> 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(dir s)
print "\t[Files in %s]" % filepath
print "\n\t".join(fil es)
print "=" * 50
HTH,
-tkc
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(dir s) + '\n'
print "\t[Files in %s]" % filepath
print '\t' + "\n\t".join(fil es)
print "=" * 50
>> 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
> 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
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(dirna me).walkfiles(' *.foo'):
# process a .foo file here http://www.jorendorff.com/articles/p...ath/index.html
Kent
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(dirna me).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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 = opendir($dirPath);
|
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...
|
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 following for a given Form:
1)Container
|
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 say strFilter, but seem
to have trouble with the correct syntax / column property
For Each strItem In cntl.ItemsSelected
strSQL = strSQL &...
|
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, files in os.walk('/directory/'):
print root
# print dirs
# print files
| |
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 to synchronise two directories (just
backup for now), but cannot see how I can traverse a second one. I do this
now with os.listdir() recursively,...
|
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
|
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
|
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):
DC_List.append((dirname,''))
os.path.walk('\\\\vcdcflx006\\Flex\\Sites', walker1, 0)
|
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...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
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: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |