I'm using the package subprocess as I used before:
from subprocess import check_call, STDOUT
import os
import sys
for file in os.listdir('directory/'):
if '1' in file:
for file_ in os.listdir('directory/'):
if '2' in file_:
command = f"cat directory/file1 directory/file2 > directory/file3"
check_call(command.split(), stdout=sys.stdout, stderr=STDOUT)
The entire error message:
cat: >: No such file or directory
cat: directory/file3: No such file or directory
Traceback (most recent call last):
File "/home/script.py", line 12, in <module>
check_call(command.split(), stdout=sys.stdout, stderr=STDOUT)
File "/usr/lib64/python3.6/subprocess.py", line 311, in check_call
raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['cat', 'directory/file1', 'directory/file2', '>', 'directory/file3']' returned non-zero exit status 1.
When I run the command
cat directory/file1 directory/file2 > directory/file3
on Linux it works fine.
Anyone has an idea what could be the problem?
CodePudding user response:
check_call does not support shell operations like ">" you will need a sub shell if you want to do that:
import subprocess
import os
for file in os.listdir('directory/'):
if '1' in file:
for file_ in os.listdir('directory/'):
if '2' in file_:
command = f"cat directory/file1 directory/file2 > directory/file3"
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
out, err = process.communicate()
print("STDOUT:" str(out))
