473,408 Members | 2,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

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.

Nov 3 '05 #1
3 2027
"hungbichvo" <hu********@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.

Nov 3 '05 #2
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()
Nov 3 '05 #3
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()
Nov 3 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: gl | last post by:
I'm trying to delete a directory that contains readonly files. Is there any easy way to do this? I get a System.UnauthorizedAccessException when a read only file is encountered. Is there a way to...
4
by: who be dat? | last post by:
I feel stupid for asking this but I can't figure this out. I've got some text files I want my website to read. The text files are located in a subdirectory of my application. Physically, the...
2
by: wstsoi | last post by:
hi I have to read images from spreadsheet, is it possible to do with php?
4
by: Jim | last post by:
I have the following piece of code: Directory.CreateDirectory(myDir); DirectoryInfo lDir = new DirectoryInfo(myDir); lDir.Attributes = FileAttributes.Normal; When this code runs, my directory...
1
by: Claire | last post by:
Ive written a small string resource building utility that I send out to our translators. I have a setup project for each language we support, which picks out a group of 12 english resx files plus...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.