Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/26.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.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命令_Python_Linux_Shell_Subprocess - Fatal编程技术网

无法使用python子流程模块运行带*的shell命令

无法使用python子流程模块运行带*的shell命令,python,linux,shell,subprocess,Python,Linux,Shell,Subprocess,我无法使用python子流程模块运行任何命令,该模块在命令中包含*符号。 我用这种方式打电话 subprocess.Popen( 'cp /etc/varnida_sys/* /tmp/bucket/'.split(), stdout=subprocess.PIPE).communicate()[0] 为此我得到了 cp: cannot stat ‘/etc/varnida_sys/*’: No such file or directory

我无法使用python子流程模块运行任何命令,该模块在命令中包含
*
符号。 我用这种方式打电话

 subprocess.Popen(
            'cp /etc/varnida_sys/* /tmp/bucket/'.split(),
            stdout=subprocess.PIPE).communicate()[0]
为此我得到了

cp: cannot stat ‘/etc/varnida_sys/*’: No such file or directory
为什么会出现此错误,在/etc/varnida\u sys/genders中有一个文件

我的调查表明使用regex like*需要一些特殊处理。我在所有包含*的命令中都遇到了一些错误


另外,当我从远程主机通过paramiko运行相同的命令时,我没有收到错误

*
仅由shell(它将其扩展为文件列表)理解,您需要将
shell=True
传递给
Popen()
。此外,无需拆分命令,您可以使用字符串:

subprocess.Popen("cp /etc/varnida_sys/* /tmp/bucket/",
                 stdout=subprocess.PIPE, shell=True).communicate()[0]
正如@triplee在下面所建议的,最好使用一些方便的包装器来完成此任务,例如:


*
只有shell才能理解(将其扩展为文件列表),您需要将
shell=True
传递给
Popen()
。此外,无需拆分命令,您可以使用字符串:

subprocess.Popen("cp /etc/varnida_sys/* /tmp/bucket/",
                 stdout=subprocess.PIPE, shell=True).communicate()[0]
正如@triplee在下面所建议的,最好使用一些方便的包装器来完成此任务,例如:


补充。它在Linux上不起作用(正如eugene所解释的那样),但在Windows上起作用(用
copy
替换
cp
,或用
powershell-c cp运行…
)。已添加。它在Linux上不起作用(正如eugene所解释的那样),但在Windows上起作用(用
copy
替换
cp
,或者用
powershell-Ccp运行…
)。但是,您需要更简单的
子流程。call()
。对
shell=True
的要求仍然有效。@tripleee:从子进程调用()上的
:不要将stdout=PIPE或stderr=PIPE与此函数一起使用。如果子进程生成足够多的输出到管道,以填充操作系统管道缓冲区,则子进程将阻塞,因为没有从中读取管道。您希望得到什么输出?应该没有;或者使用
子流程。改为检查输出()。我的观点是,
Popen
对于琐碎的任务来说是不必要的复杂;这就是为什么对于常见场景有许多方便的包装器。但是,您需要更简单的
subprocess.call()
。对
shell=True
的要求仍然有效。@tripleee:从子进程调用()上的
:不要将stdout=PIPE或stderr=PIPE与此函数一起使用。如果子进程生成足够多的输出到管道,以填充操作系统管道缓冲区,则子进程将阻塞,因为没有从中读取管道。您希望得到什么输出?应该没有;或者使用
子流程。改为检查输出()。我的观点是,
Popen
对于琐碎的任务来说是不必要的复杂;这就是为什么对于常见场景有许多方便的包装器。