Moving from perl to python: questions

David R. Favor dfavor at austin.ibm.com
Wed Feb 16 09:39:04 EST 2000


Gaetan Corneau wrote:
> 
> David,
> 
> >   1) Best way to convert array argv[1:] into string to pass functions
> 
> from string import join
> 
> l = ["aaa", "bbb", "ccc"]
> 
> s = join(l[1:])
> 
> >   2) How to implement a no-frills file slurp() function
> 
> What is a "slurp" function?

Hum... The attachment of my code didn't make it. Let's try this again...
-------------- next part --------------
#!/usr/local/bin/python

def slurp(filename):

   "Slurp all the lines of a file into an array"

   print filename

   try:
      f = open(filename)

   lines = f.readlines()

   f.close()

   return lines

def chomp(lines):
   "remove the end of line markers from array"
   import os
   nl_len = len(os.linesep)   # length of os dependent line seperator
   while i < len(lines):
      line_len = len(lines[i])
      lines[i] = lines[i][:line_len-nl_len]

if __name__ == '__main__':

   from sys import argv
   #from getopt import *

   #opts = getopt(sys.argv[:1],'f:')

   lines = slurp(str(argv[1:]))

   print lines


More information about the Python-list mailing list