473,399 Members | 2,774 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,399 software developers and data experts.

recursing through files in a folder

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
Jul 18 '05 #1
3 1835
Scott Carlson wrote:
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
Jul 18 '05 #2
Scott Carlson wrote:
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
Jul 18 '05 #3
Larry Bates said the following on 10/01/2004 04:20 PM:
Scott Carlson wrote:
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


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
Jul 18 '05 #4

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

Similar topics

2
by: Glen | last post by:
As I understand it, when the first page of an application is accessed, all ASPX/ASCX/etc. files in the same folder are compiled using the JIT compiler. Is there a way to turn this feature off? ...
8
by: vinesh | last post by:
I have sample Asp.Net Web Application project. Let me know how to keep the files related to this project (like the webform.aspx, WebForm1.aspx.vb, WebForm1.aspx.resx) in a separate folder within a...
8
by: Paw | last post by:
Greetings. I use asp. what I need is is when a visitor comes to the site, I need it to check the host name. if "www.hometowndigest.com" is the host, then check a folder named "something" and if...
3
by: sreemakam | last post by:
Hello, I am a li'l new to perl scripting and I am trying to write a script which can give me the result in a particular format. I need to generate all the directories as well as the subdirectories...
3
by: half.italian | last post by:
I'm making a small interface for copying large groups of files around a filesystem. I have a progressbar that counts the items in the destination, and increments as each new file is copied over. ...
3
by: jaeden99 | last post by:
I was wandering if nyone has a script to move files older than x days old? i've seen several to delete, but I don't want to delete. I would like to create a backup of the files first verify with...
9
by: OWeb | last post by:
Javascript and recursing subfolders assistance ------------------------- I have this script that is a free extra download from SlideShowPro. It's a great script but I feel it needs to be...
4
by: Edwin Velez | last post by:
http://msdn.microsoft.com/en-us/library/806sc8c5.aspx The URL above gives sample code for use within a Console Application. What I would like to do is use this code within a Windows Form. That...
8
by: =?Utf-8?B?QnJ5YW4=?= | last post by:
Hello group. I have some code (given to me), but I don't know alot about ASP, so I was hoping someone here can help. Running on Win 2008 server. The code below will scan a folder and subfolder...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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,...
0
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...

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.