Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reproduce the Unix cat command in Python

Tags:

python

cat

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

like image 651
JBWhitmore Avatar asked Jul 18 '12 01:07

JBWhitmore


People also ask

Can I use cat command in Python?

The cat command is a Linux shell command as discussed above, it cannot be executed in Windows Command-Line. However, when using the same through python's API, the command can be executed in windows as well as Linux in the same manner.

What is Unix command in Python?

Python. Unix is an operating system which was developed in around 1969 at AT&T Bell Labs by Ken Thompson and Dennis Ritchie. There are many interesting Unix commands we can use to carry out different tasks.

What is cat command Unix?

Description. The cat command reads one or more files and prints their contents to standard output. Files are read and output in the order they appear in the command arguments.

What is cat command in shell script?

Cat(concatenate) command is very frequently used in Linux. It reads data from the file and gives their content as output. It helps us to create, view, concatenate files. So let us see some frequently used cat commands. 1) To view a single file.


1 Answers

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)
like image 169
Jonathan Callen Avatar answered Oct 17 '22 17:10

Jonathan Callen