| View previous topic :: View next topic |
| Author |
Message |
PhilC
Joined: Tue Apr 26, 2005 6:08 pm Posts: 6
|
Posted: Wed Aug 17, 2005 4:16 pm Post subject: Opening and processing files in Python |
|
|
Hi,
I'm trying to make a start in Python programming. I consider myself competant with Perl, and I want to do some of the same things which are easy in Perl, in Python.
In particular, I want to be open a file and process each line at a time.
In Perl I just do:
while (<>) {
doSomethingWith ($_);
}
I've had a browse around python.org, but can't find any good references to opening and processing files.
Can anyone tell me how to do the above Perl code in Python? I really would appreciate it, thanks. |
|
| Back to top |
|
 |
nelz Moderator

Joined: Mon Apr 04, 2005 12:52 pm Posts: 8000 Location: Warrington, UK
|
Posted: Wed Aug 17, 2005 6:35 pm Post subject: RE: Opening and processing files in Python |
|
|
| Code: |
lines = open('foo/bar').readlines()
for line in lines:
dosomethingwith(line)
|
_________________ Unix is user-friendly. It's just very selective about who it's friends are. |
|
| Back to top |
|
 |
PhilC
Joined: Tue Apr 26, 2005 6:08 pm Posts: 6
|
Posted: Thu Aug 18, 2005 1:34 pm Post subject: RE: Opening and processing files in Python |
|
|
Thanks for getting me started nelz, I'll be using that a lot in the future.
I did a little bit more digging though since the perl diamond operator can do a little bit more than this code.
i.e. it will read each file given as an argument to the program and process each file in turn. Much more useful than that though is that it can read input from stdin or a pipe.
Just in case anyone else is looking for this feature in Python, I had another look around python.org and found this code: (http://www.python.org/doc/2.2.3/lib/module-fileinput.html)
import fileinput
for line in fileinput.input():
dosomethingwith (line) |
|
| Back to top |
|
 |
overflow LXF regular

Joined: Tue Aug 23, 2005 2:40 pm Posts: 158 Location: London
|
Posted: Wed Aug 24, 2005 12:40 pm Post subject: |
|
|
It is even easier than the example posted above.
| Code: |
for line in file('foo.bar'):
print line
|
file (which is a synonym for open) support iterators and it yields a class which simply passes records back to you one at a time.
The example posted above would read the whole file into memory in the readlines() function which might be a performance problem for large files. _________________ overflow |
|
| Back to top |
|
 |
overflow LXF regular

Joined: Tue Aug 23, 2005 2:40 pm Posts: 158 Location: London
|
Posted: Wed Aug 24, 2005 1:00 pm Post subject: |
|
|
Python doesn't have the equivalent of the Perl <> operator but it's easy enough to reproduce it. Here's a simple cat program written in Python which supports multiple filenames on the command line and '-' for stdin.
| Code: | #! /usr/bin/python
import sys
def fileargs():
for filename in sys.argv[1:]:
if filename == '-':
for line in sys.stdin:
yield line
else:
for line in file(filename):
yield line
for line in fileargs():
print line
|
The fileargs function is an iterator (or generator, I can't remember the difference) and passes back records from each input file in turn. It switches to stdin if '-' is specified.
I hope that helps. _________________ overflow |
|
| Back to top |
|
 |
PhilC
Joined: Tue Apr 26, 2005 6:08 pm Posts: 6
|
Posted: Tue Oct 04, 2005 1:51 pm Post subject: |
|
|
Thanks overflow,
That was a very insightful answer and a great example to demonstrate.
(Still wondering why people prefer to use this to perl though) |
|
| Back to top |
|
 |
nelz Moderator

Joined: Mon Apr 04, 2005 12:52 pm Posts: 8000 Location: Warrington, UK
|
Posted: Tue Oct 04, 2005 2:01 pm Post subject: |
|
|
Because the code doesn't look like line noise  _________________ Unix is user-friendly. It's just very selective about who it's friends are. |
|
| Back to top |
|
 |
| View previous topic :: View next topic |
|