How to read all files in a directory

H

hungbichvo

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.
 
S

Stefan Arentz

hungbichvo said:
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.['hello.rb', 'test.rb', 'ping.rb', 'echo.rb']

Sorry for the ruby code in the example :)

S.
 
L

Larry Bates

Not tested:

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



-Larry Bates
 
L

Larry Bates

Not tested:

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



-Larry Bates
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top