Moving from perl to python: questions

Alan Daniels daniels at mindspring.com
Wed Feb 16 19:46:14 EST 2000


On Wed, 16 Feb 2000 08:15:09 -0600, the infinitely wise David R. Favor
(dfavor at austin.ibm.com) spoke forth to us, saying...

[snip...]
>  1) Best way to convert array argv[1:] into string to pass functions

Easiest way to get all the lines in a file in one shot is:
lines = open("some_file.txt").readlines()

Wether you want to wrap this in a function to gracefully catch any
exceptions (such as the file not existing) is up to you.

>  2) How to implement a no-frills file slurp() function

A quick way to do a Perl-style chomp (I've used this before in
production code and it seemed to work well enough):

def chomp(x):
    while x != "" and x[-1] in "\r\n":
        x = x[:-1]
    return x

Hope this helps.

-- 
=======================
Alan Daniels
daniels at mindspring.com
daniels at cc.gatech.edu



More information about the Python-list mailing list