working with files with the same extenstion
Ruediger Maehl
ruediger.maehl_nospam at web.de
Wed Feb 6 10:06:38 EST 2002
"Ulrich Pfisterer" <ulrich.pfisterer at rmb.co.za> schrieb im Newsbeitrag
news:3C5EAF50.80004 at rmb.co.za...
> 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
Welcome to Python.
You may prefer perhaps a more old-fashioned style like:
# -----
# file_ext.py
import sys
import os
directory = sys.argv[1]
extension = sys.argv[2]
for fn in os.listdir(directory):
if extension.lower() == os.path.splitext(fn)[1].lower():
print fn
# -----
And you would call that using the following command in a
DOS box:
python file_ext.py c:\temp .txt
This program loops through _every_ file in the directory,
but also gives you the possibilty to distinguish files and
directories (which may also have extensions).
HTH,
Rüdiger
More information about the Python-list
mailing list