recursing through files in a folder

S

Scott Carlson

New to Python. Very new.

I need to pass a parameter to the program I create that points to a
folder. (How are params passed in?)

Then, I need to be able to scroll through each file in the folder,
looking for files with a particular naming convention.

What is the best way to be able to look at each file in a folder and
decide if I wish to use that one, and then go to the next file?

Thanks,
Scott
 
S

Steve Holden

Scott said:
New to Python. Very new.

I need to pass a parameter to the program I create that points to a
folder. (How are params passed in?)
The sys module has an argv member, with the usual convention that the
program name is argument zero.

So if you just add one directory (sorry, folder) name to the command
line you need something like:

myFolder = sys.argv[1]
Then, I need to be able to scroll through each file in the folder,
looking for files with a particular naming convention.

What is the best way to be able to look at each file in a folder and
decide if I wish to use that one, and then go to the next file?
The easiest way would be to use the "glob" module, specifically made for
tricks like that. Functions in os.path can be useful to keep your code
portable, too. Suppose you wanted a list of Windows executables in the
folder, you'd use something like:

#!/usr/bin/python
import sys, glob, os.path

folder = sys.argv[1]
pattern = "*.exe"
filenames = glob.glob(os.path.join(folder, pattern))
for f in filenames:
print f

and run it like so:

C:\Steve>files.py C:\Installers
C:\Installers\AdbeRdr60_enu_full.exe
C:\Installers\awrdpr210_setup.exe
.
.
.
C:\Installers\winzip90.exe
C:\Installers\wxPythonWIN32-2.5.1.5-Py23.exe

or, in Unix-like environments:
$ ./files.py /c/Installers
/c/Installers/AdbeRdr60_enu_full.exe
/c/Installers/awrdpr210_setup.exe
.
.
.
/c/Installers/winzip90.exe
/c/Installers/wxPythonWIN32-2.5.1.5-Py23.exe


regards
Steve
 
L

Larry Bates

Scott said:
New to Python. Very new.

I need to pass a parameter to the program I create that points to a
folder. (How are params passed in?)

Then, I need to be able to scroll through each file in the folder,
looking for files with a particular naming convention.

What is the best way to be able to look at each file in a folder and
decide if I wish to use that one, and then go to the next file?

Thanks,
Scott

Use getopt to pick up the root directory from call line
Use os.path.walk to walk each of the files/directories
Use glob.glob to get a list of files that match

Larry Bates
 
M

Mirko Zeibig

Larry Bates said the following on 10/01/2004 04:20 PM:
Use getopt to pick up the root directory from call line
Use os.path.walk to walk each of the files/directories
Use glob.glob to get a list of files that match

I'd opt for `optparse` instead of `getopt` ;-). Since Python 2.3 it's
included in the distribution, for older version search for `optik`,
which is the old name of `optparse`.

Regards
Mirko
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,479
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top