从 python 脚本使用 cat 命令时收到错误消息 "raise CalledProcessError(retcode, cmd)"
Getting the error message "raise CalledProcessError(retcode, cmd)" when using the cat command from a python script
我正在使用我之前使用的包子流程:
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)
整个错误信息:
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.
当我运行命令
cat directory/file1 directory/file2 > directory/file3
在 Linux 上工作正常。
有人知道问题出在哪里吗?
check_call 不支持像“>”这样的 shell 操作,如果你想这样做,你需要一个子 shell:
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))
我正在使用我之前使用的包子流程:
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)
整个错误信息:
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.
当我运行命令
cat directory/file1 directory/file2 > directory/file3
在 Linux 上工作正常。
有人知道问题出在哪里吗?
check_call 不支持像“>”这样的 shell 操作,如果你想这样做,你需要一个子 shell:
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))