List of files to be opened

Y

yawgmoth7

Hello, I am currently writing a script that requires a few different
files to be opened, and examined. What I need to be able to do is use
something like:

filelist = os.system("ls")
<Some way to open the file list and read each file one by one here
I cannot think of a way to do this, I could put them in a list of
something of the sort. But that still does not solve the problem of
opening them one by one.

Thanks for all the advice and help.
 
J

James Stroud

yawgmoth7 said:
Hello, I am currently writing a script that requires a few different
files to be opened, and examined. What I need to be able to do is use
something like:

filelist = os.system("ls")
<Some way to open the file list and read each file one by one here


I cannot think of a way to do this, I could put them in a list of
something of the sort. But that still does not solve the problem of
opening them one by one.

Thanks for all the advice and help.


See os, os.path. Then check out the fileinput module.

A common pattern is:

for afilename in os.listdir(apath):
for aline in open(os.path.join(apath, afilename)):
# do something with aline

This allows you to do something with each line in a file.

Here are some urls that you will find helpful for these kind of things:

http://docs.python.org/api/fileObjects.html
http://docs.python.org/lib/module-fileinput.html
http://docs.python.org/lib/os-file-dir.html
http://docs.python.org/lib/module-os.path.html

James
 
J

James Stroud

yawgmoth7 said:
Hello, I am currently writing a script that requires a few different
files to be opened, and examined. What I need to be able to do is use
something like:

filelist = os.system("ls")
<Some way to open the file list and read each file one by one here


I cannot think of a way to do this, I could put them in a list of
something of the sort. But that still does not solve the problem of
opening them one by one.

Thanks for all the advice and help.


See os, os.path. Then check out the fileinput module.

A common pattern is:

for afilename in os.listdir(apath):
if os.path.isfile(os.path.join(apath, afilename)):
for aline in open(os.path.join(apath, afilename)):
# do something with aline

This allows you to do something with each line of each file in a directory.

Here are some urls that you will find helpful for these kind of things:

http://docs.python.org/api/fileObjects.html
http://docs.python.org/lib/module-fileinput.html
http://docs.python.org/lib/os-file-dir.html
http://docs.python.org/lib/module-os.path.html

James
 
K

Ken Starks

yawgmoth7 said:
Hello, I am currently writing a script that requires a few different
files to be opened, and examined. What I need to be able to do is use
something like:

filelist = os.system("ls")
<Some way to open the file list and read each file one by one here

I cannot think of a way to do this, I could put them in a list of
something of the sort. But that still does not solve the problem of
opening them one by one.

Thanks for all the advice and help.

os.walk is your friend. Its has wonderful functionality.

The documentation is in the subsection 'Files and Directories' of the os
module, and there are a couple of examples.

Global Module index --> os --> 'Files and Directories' (Section 6.1.4)
-->Bottom of page
 
C

Carl J. Van Arsdall

Ken said:
yawgmoth7 wrote:



os.walk is your friend. Its has wonderful functionality.
Don't you mean os.path.walk ?

The documentation is in the subsection 'Files and Directories' of the os
module, and there are a couple of examples.

Global Module index --> os --> 'Files and Directories' (Section 6.1.4)
-->Bottom of page


--

Carl J. Van Arsdall
(e-mail address removed)
Build and Release
MontaVista Software
 
F

Fredrik Lundh

Carl said:
Don't you mean os.path.walk ?

os.walk is a generator-based version of os.path.walk. instead of putting
the logic in a callback function, you put it in a for loop:

for root, dirs, files in os.walk(top):
for file in files:
file = os.path.join(root, file)
print file, "..."

os.listdir and glob.glob are still good choices if you just want the files in
a given directory.

</F>
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top