473,382 Members | 1,255 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,382 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 1192
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.
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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...

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.