Newbie question: files..

Sonny Parlin sparlin at openpro.org
Sat Oct 9 21:49:25 EDT 1999


Hello, I'm trying to learn python by setting up some pretty easy examples
of file manipulation...

I have a file of 1000 lines that I'm simply trying to read in and display
using python. The code I have is here:

#!/usr/bin/python

file = open('testfile.txt', 'r')
for line in file.readlines(): # read file
    s = line[:-1] # get rid of newline
    print s #print string
file.close()

I think I have some unnecessary steps in that program, because here is the
Perl equivalent:

#!/usr/bin/perl

open(FD, "testfile.txt");
while(<FD>) {
    print $_;
}
close(FD);

The Perl example is smaller and runs a few milliseconds faster, so could
someone please point out the deficiencies in my python script?

Thanks

-Sonny
-- 
Sonny Parlin
sparlin at openpro.org
http://www.openpro.org
fingerprint = 45 FE 56 72 C2 04 CD 76  
		09 75 52 7A C6 7C 23 D2




More information about the Python-list mailing list