473,406 Members | 2,769 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,406 software developers and data experts.

List of files to be opened

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.
--
gurusnetwork.org
Gurus'Network - Are you a guru?
Jan 26 '06 #1
5 1193
yawgmoth7 wrote:
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.
--
gurusnetwork.org
Gurus'Network - Are you a guru?

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
Jan 26 '06 #2
yawgmoth7 wrote:
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.
--
gurusnetwork.org
Gurus'Network - Are you a guru?

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
Jan 26 '06 #3
yawgmoth7 wrote:
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.
--
gurusnetwork.org
Gurus'Network - Are you a guru?


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
Jan 26 '06 #4
Ken Starks wrote:
yawgmoth7 wrote:

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.
--
gurusnetwork.org
Gurus'Network - Are you a guru?

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
cv*********@mvista.com
Build and Release
MontaVista Software

Jan 26 '06 #5
Carl J. Van Arsdall wrote:
os.walk is your friend. Its has wonderful functionality.


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>

Jan 26 '06 #6

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

Similar topics

5
by: Vinicius | last post by:
Hi, I have this script to list files in the same directory; <? $dir=opendir("."); readdir($dir); readdir($dir); while ($conteudo = readdir($dir)) { echo "<a...
10
by: Bulba! | last post by:
Hello everyone, I'm reading the rows from a CSV file. csv.DictReader puts those rows into dictionaries. The actual files contain old and new translations of software strings. The dictionary...
8
by: vinesh | last post by:
I have sample Asp.Net Web Application project. Let me know how to keep the files related to this project (like the webform.aspx, WebForm1.aspx.vb, WebForm1.aspx.resx) in a separate folder within a...
20
by: John Salerno | last post by:
Here's the code I wrote: file = open('C:\switches.txt', 'r') switches = file.readlines() i = 0 for line in switches: line = switches i += 1
0
by: ebferro | last post by:
Forgive me because this is going to be such a simple question but I'm new and at the foot of the learning curve of VB. I am trying to develop a Windows application that will present the user with...
4
by: zacks | last post by:
Most applications whose purpose is to work with various types of files implement a "Most Recent Files" list, where the last, say, four files accessed by the application can quickly be re-opened by...
10
by: lancer6238 | last post by:
Hi all, I'm having programs reading from files. I have a text file "files.txt" that contains the names of the files to be opened, i.e. the contents of files.txt are Homo_sapiens.fa...
1
by: radu587 | last post by:
Is there a way in which I could list all opened files in windows system? I want to check when a file that was opened for editing in word or notepad is still opened. Do you have any idea how I can do...
9
by: Bruno GUERPILLON | last post by:
Hi, I'd like, in a WIN32 environment, list all open files. Anyone got a clue how to do this ? Regards, Bruno.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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.