Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python子进程。检查常规shell命令的\u output()_Python_Bash_Subprocess - Fatal编程技术网

Python子进程。检查常规shell命令的\u output()

Python子进程。检查常规shell命令的\u output(),python,bash,subprocess,Python,Bash,Subprocess,我正在为SQL表开发一个简单的Tkinter GUI。显然,我不得不使用shell命令,而且我一直在努力使用子进程shell系统 例如,subprocess.check_output('ls')将运行ls,但要运行ls-l,您需要使用subprocess.check_output(['ls','-l'])。我没有找到获取更复杂命令的输出的方法,例如cat test.sql | sqlite3 test.db(在test.db上运行sqlite3,然后在提示符处列出test.sql) 我尝试过的事

我正在为SQL表开发一个简单的Tkinter GUI。显然,我不得不使用shell命令,而且我一直在努力使用子进程shell系统

例如,
subprocess.check_output('ls')
将运行
ls
,但要运行
ls-l
,您需要使用
subprocess.check_output(['ls','-l'])
。我没有找到获取更复杂命令的输出的方法,例如
cat test.sql | sqlite3 test.db
(在
test.db
上运行
sqlite3
,然后在提示符处列出
test.sql

我尝试过的事情
  • subprocess.check_输出(['cat','test.sql','|','sqlite3','test.db'])

    错误(在Python shell上运行):

  • 使用
    bash-c
    命令:
    subprocess.check_输出(['bash','-c','cat test.sql | sqlite3 test.db'))

    错误:

    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/usr/lib/python3.7/subprocess.py", line 395, in check_output
        **kwargs).stdout
      File "/usr/lib/python3.7/subprocess.py", line 472, in run
        with Popen(*popenargs, **kwargs) as process:
      File "/usr/lib/python3.7/subprocess.py", line 775, in __init__
        restore_signals, start_new_session)
      File "/usr/lib/python3.7/subprocess.py", line 1436, in _execute_child
        executable = os.fsencode(executable)
      File "/usr/lib/python3.7/os.py", line 809, in fsencode
        filename = fspath(filename)  # Does type-checking of `filename`.
    TypeError: expected str, bytes or os.PathLike object, not list
    
    bash: cat test.sql | sqlite3 test.db: command not found
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/usr/lib/python3.7/subprocess.py", line 395, in check_output
        **kwargs).stdout
      File "/usr/lib/python3.7/subprocess.py", line 487, in run
        output=stdout, stderr=stderr)
    subprocess.CalledProcessError: Command '['bash', '-c', '"cat test.sql | sqlite3 test.db"']' returned non-zero exit status 127.
    
    bash:cat test.sql | sqlite3 test.db:未找到命令
    回溯(最近一次呼叫最后一次):
    文件“”,第1行,在
    文件“/usr/lib/python3.7/subprocess.py”,第395行,在check_输出中
    **kwargs)stdout
    文件“/usr/lib/python3.7/subprocess.py”,第487行,正在运行
    输出=标准输出,标准输出=标准输出)
    subprocess.CalledProcessError:命令'['bash','-c',''cat test.sql | sqlite3 test.db']'返回非零退出状态127。
    
    (请注意,单独运行
    bash-c“cat test.sql | sqlite3 test.db”
    工作得非常好。)


最后我只使用了
os.system()
os.popen()
命令,但人们说不推荐使用这些命令。我该怎么办?

查看
子流程。检查输出文件,您有两个选项:

output=subprocess。检查输出(“cat test.sql | sqlite3 test.db”,shell=True)


查看
子流程。检查输出文件,您有两个选项:

output=subprocess。检查输出(“cat test.sql | sqlite3 test.db”,shell=True)

bash: cat test.sql | sqlite3 test.db: command not found
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.7/subprocess.py", line 395, in check_output
    **kwargs).stdout
  File "/usr/lib/python3.7/subprocess.py", line 487, in run
    output=stdout, stderr=stderr)
subprocess.CalledProcessError: Command '['bash', '-c', '"cat test.sql | sqlite3 test.db"']' returned non-zero exit status 127.
from subprocess import Popen, PIPE

p1 = Popen(["cat", "test.sql"], stdout=PIPE)
p2 = Popen(["sqlite3", "test.db"], stdin=p1.stdout, stdout=PIPE)
p1.stdout.close()  # Allow p1 to receive a SIGPIPE if p2 exits.
output = p2.communicate()[0]