Reproduce the Unix cat command in Python
Question: I am currently reproducing the following Unix command:

    
    
    cat command.info fort.13 > command.fort.13
    

in Python with the following:

    
    
    with open('command.fort.13', 'w') as outFile:
      with open('fort.13', 'r') as fort13, open('command.info', 'r') as com:
        for line in com.read().split('\n'):
          if line.strip() != '':
            print >>outFile, line
        for line in fort13.read().split('\n'):
          if line.strip() != '':
            print >>outFile, line
    

which works, but there has to be a better way. Any suggestions?

Edit (2016):

This question has started getting attention again after four years. I wrote up
some thoughts in a longer Jupyter Notebook
[here](https://github.com/jbwhit/one-
offs/blob/master/notebooks/2016-09-19_jbw_stackoverflow.ipynb).

The crux of the issue is that my question was pertaining to the (unexpected by
me) behavior of `readlines`. The answer I was aiming toward could have been
better asked, and that question would have been better answered with
`read().splitlines()`.


Answer: The easiest way might be simply to forget about the lines, and just read in
the entire file, then write it to the output:

    
    
    with open('command.fort.13', 'wb') as outFile:
        with open('command.info', 'rb') as com, open('fort.13', 'rb') as fort13:
            outFile.write(com.read())
            outFile.write(fort13.read())
    

As pointed out in a comment, this can cause high memory usage if either of the
inputs is large (as it copies the entire file into memory first). If this
might be an issue, the following will work just as well (by copying the input
files in chunks):

    
    
    import shutil
    with open('command.fort.13', 'wb') as outFile:
        with open('command.info', 'rb') as com, open('fort.13', 'rb') as fort13:
            shutil.copyfileobj(com, outFile)
            shutil.copyfileobj(fort13, outFile)
    

