Linux Format forums Forum Index Linux Format forums
Help, discussion, magazine feedback and more
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Opening and processing files in Python

 
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming
View previous topic :: View next topic  
Author Message
PhilC



Joined: Tue Apr 26, 2005 6:08 pm
Posts: 6

PostPosted: Wed Aug 17, 2005 4:16 pm    Post subject: Opening and processing files in Python Reply with quote

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
View user's profile Send private message
nelz
Site admin


Joined: Mon Apr 04, 2005 12:52 pm
Posts: 8450
Location: Warrington, UK

PostPosted: Wed Aug 17, 2005 6:35 pm    Post subject: RE: Opening and processing files in Python Reply with quote

Code:

lines = open('foo/bar').readlines()
for line in lines:
   dosomethingwith(line)

_________________
"Insanity: doing the same thing over and over again and expecting different results." (Albert Einstein)
Back to top
View user's profile Send private message
PhilC



Joined: Tue Apr 26, 2005 6:08 pm
Posts: 6

PostPosted: Thu Aug 18, 2005 1:34 pm    Post subject: RE: Opening and processing files in Python Reply with quote

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
View user's profile Send private message
overflow
LXF regular


Joined: Tue Aug 23, 2005 2:40 pm
Posts: 158
Location: London

PostPosted: Wed Aug 24, 2005 12:40 pm    Post subject: Reply with quote

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
View user's profile Send private message
overflow
LXF regular


Joined: Tue Aug 23, 2005 2:40 pm
Posts: 158
Location: London

PostPosted: Wed Aug 24, 2005 1:00 pm    Post subject: Reply with quote

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
View user's profile Send private message
PhilC



Joined: Tue Apr 26, 2005 6:08 pm
Posts: 6

PostPosted: Tue Oct 04, 2005 1:51 pm    Post subject: Reply with quote

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
View user's profile Send private message
nelz
Site admin


Joined: Mon Apr 04, 2005 12:52 pm
Posts: 8450
Location: Warrington, UK

PostPosted: Tue Oct 04, 2005 2:01 pm    Post subject: Reply with quote

Because the code doesn't look like line noise Smile
_________________
"Insanity: doing the same thing over and over again and expecting different results." (Albert Einstein)
Back to top
View user's profile Send private message
View previous topic :: View next topic  
Display posts from previous:   
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Linux Format forums topic RSS feed 


Powered by phpBB © 2001, 2005 phpBB Group


Copyright 2011 Future Publishing, all rights reserved.


Web hosting by UKFast