How to read all files in a directory

Larry Bates larry.bates at
Thu Nov 3 23:14:20 CET 2005

Not tested:

import glob
import os
for fileName in glob.glob(os.path.join(path,'*.DAT')):
    dataFile=open(fileName, 'r').readlines()
    . Continue yur code here

-Larry Bates
hungbichvo wrote:
> 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.

More information about the Python-list mailing list