Using greater than operator with subprocess.call

> output redirection is a shell feature, but subprocess.call() with an args list and shell=False (the default) does not use a shell.

You’ll have to use shell=True here:

subprocess.call("cat /path/to/file_A > file_B", shell=True)

or better still, use subprocess to redirect the output of a command to a file:

with open('file_B', 'w') as outfile:
    subprocess.call(["cat", "/path/to/file_A"], stdout=outfile)

If you are simply copying a file, use the shutil.copyfile() function to have Python copy the file across:

import shutil

shutil.copyfile('/path/to/file_A', 'file_B')

Leave a Comment