Newbie question: files..
Tim Peters
tim_one at email.msn.com
Sun Oct 10 00:51:45 EDT 1999
[Sonny Parlin]
> 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?
Your Perl is much too wordy <wink>:
open(F,"testfile.txt");print <F>;
Others will respond with trivial simplifications to the Python code too, but
who cares? Nobody wins a Fabulous Prize for saving a keystroke.
gotta-save-3-just-to-qualify-ly y'rs - tim
More information about the Python-list
mailing list