Reproduce the Unix cat command in Python

Viewed 51473

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.

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().

6 Answers
Related