How to read all files in a directory

Stefan Arentz stefan.arentz at gmail.com
Thu Nov 3 07:02:22 EST 2005


"hungbichvo" <hungbichvo at yahoo.com> writes:

> Dear All,
> 
> My python application is small. It reads data from a file.
> My code is:
>    fileName = '900128.DAT'
>    dataFile = open(fileName, 'r').readlines()
> I have to run 100 input files .DAT. Each time I run application, I have 
> to change code fileName to a new one. For example, fileName 
> = 'NewFile.DAT'.
> I do not know how I can process all file with extension .DAT in a 
> specific directory once only.
> 
> Any suggestion will be appreciated,
> 
> Thank you.

http://www.python.org/doc/2.4.2/lib/module-glob.html

% python
Python 2.4.2 (#2, Sep 30 2005, 21:19:01)
[GCC 4.0.2 20050808 (prerelease) (Ubuntu 4.0.1-4ubuntu8)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import glob
>>> print glob.glob('*.rb')
['hello.rb', 'test.rb', 'ping.rb', 'echo.rb']

Sorry for the ruby code in the example :-)

 S.




More information about the Python-list mailing list