Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > How to read all files in a directory

Reply
Thread Tools

How to read all files in a directory

 
 
hungbichvo
Guest
Posts: n/a
 
      11-03-2005
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.



 
Reply With Quote
 
 
 
 
Stefan Arentz
Guest
Posts: n/a
 
      11-03-2005
"hungbichvo" <(E-Mail Removed)> 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-4ubuntu] 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.

 
Reply With Quote
 
 
 
 
Larry Bates
Guest
Posts: n/a
 
      11-03-2005
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
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.
>
>
>

 
Reply With Quote
 
Larry Bates
Guest
Posts: n/a
 
      11-03-2005
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
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.
>
>
>


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
show all files in the directory, but how to show directory control?? strutsng@gmail.com HTML 1 09-30-2005 07:32 AM
show all files in the directory, but how to show directory control?? strutsng@gmail.com ASP General 1 09-30-2005 07:32 AM
How to read all files present in a directory in C ? vsk C Programming 5 01-19-2005 01:26 PM
list all css files in a directory and sub directory TJS ASP .Net 1 06-23-2004 10:49 AM



Advertisments