working with files with the same extenstion

jim.vickroy jim.vickroy at noaa.gov
Mon Feb 4 13:32:01 EST 2002


Welcome, I hope you enjoy Python as much as I have.

You may wish to look at the the listdir() procedure in the os (operating
system) module to generate a list of files.

If you are using Python 2.1 or later (not sure about Python 2.0), string
data types have an associated endswith() procedure.  For example:

> filename = 'anything.txt'
> filename.endswith('.txt')
1  # true
> filename endswith('.doc')
0 # false

String slicing is also an option.  For example:
> filename[-4:]
'.txt'
> filename[:-4]
'anything'

For more sophisticated pattern matching, look at the re (regular
expressions) module.

Ulrich Pfisterer wrote:

> Can anyone please tell me how I would go about doing the following.
> I would like to write a script that would take in a command line
> parameter "*.txt". The script would then loop through every file with a
> "txt" extension and something with it.
>
> I am brand new to python (I started using it 2 days ago) so I dont know
> if there is a module that does already this, or whether I have to do
> all the wildcard matching etc myself.
>
> Any help would be much appreciated




More information about the Python-list mailing list