Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   How to read all files in a directory (http://www.velocityreviews.com/forums/t351005-how-to-read-all-files-in-a-directory.html)

hungbichvo 11-03-2005 11:59 AM

How to read all files in a directory
 
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.




Stefan Arentz 11-03-2005 12:02 PM

Re: How to read all files in a directory
 
"hungbichvo" <hungbichvo@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.


Larry Bates 11-03-2005 10:14 PM

Re: How to read all files in a directory
 
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.
>
>
>


Larry Bates 11-03-2005 10:14 PM

Re: How to read all files in a directory
 
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.
>
>
>




All times are GMT. The time now is 07:53 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.