Read file and copy to standard output.
Question: I'm trying to write a python program that will read input and copy it to
standard output (with no alterations). I've been told that it needs to operate
as a Python version of the Unix cat function. If a file cannot be opened, an
error message needs to be printed, and then the program needs to continue
processing any additional files. I am a complete beginner, and have tried my
best to scrape something together with my limited knowledge. Here is what I
have so far:

    
    
    from sys import argv, stdout, stdin, stderr
    
    if len(argv) == 1:
    
        try:
            stdout.write(raw_input(' ') + '\n')
        except:
            stderr.write ('sorry' + '\n')
            quit()
    
    else:
    
        for filename in argv[1:]:
            try:
                filehandle + open(filename)
            except IOError:
                stderr.write('Sorry, could not open', filename + '\n')
                continue
    
            f = filehandle.read()
    
            stdout.write(f)
    

I am not quite sure where to go from here.. does anyone have any advice/am I
on the right track even a little bit? Please and thank you!


Answer: This function will copy the specified file to the console line by line (in
case you later on decide to give it the ability to use the `-n` command line
option of cat)

    
    
    def catfile(fn):
      with open(fn) as f:
        for line in f:
          print line,
    

It can be called with the filename once you have established the file exists.

