Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/351.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/5/bash/17.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子进程中的ln在正常命令行中成功时失败?_Python_Bash_Subprocess_Exit Code_Ln - Fatal编程技术网

为什么Python子进程中的ln在正常命令行中成功时失败?

为什么Python子进程中的ln在正常命令行中成功时失败?,python,bash,subprocess,exit-code,ln,Python,Bash,Subprocess,Exit Code,Ln,正如标题所说: >>> from subprocess import check_output >>> check_output(['ln', '~/other_folder/src/models/sc_models.py', './src/models/sc_models.py']) Traceback (most recent call last): File "<input>", line 1, in <module> F

正如标题所说:

>>> from subprocess import check_output
>>> check_output(['ln', '~/other_folder/src/models/sc_models.py', './src/models/sc_models.py'])
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/usr/lib/python2.7/subprocess.py", line 573, in check_output
    raise CalledProcessError(retcode, cmd, output=output)
CalledProcessError: Command '['ln', '~/other_folder/src/models/sc_models.py', './src/models/sc_models.py']' returned non-zero exit status 1
>>> exit()
$ ln ~/other_folder/src/models/sc_models.py ./src/models/sc_models.py
$
来自子流程导入检查输出的
>>
>>>检查_输出(['ln','~/other_folder/src/models/sc_models.py','./src/models/sc_models.py']))
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“/usr/lib/python2.7/subprocess.py”,第573行,在check_输出中
引发被调用的进程错误(retcode,cmd,output=output)
CalledProcessError:命令'['ln','~/other_folder/src/models/sc_models.py','./src/models/sc_models.py']'返回非零退出状态1
>>>退出()
$ln~/other_文件夹/src/models/sc_models.py./src/models/sc_models.py
$
这怎么可能?它如何从命令行成功,但从Python子流程调用失败

欢迎所有提示

您需要使用:

在Unix和Windows上,返回参数时,初始组件~or~ user被该用户的主目录替换

Python正在您的cwd中寻找一个名为
~
的目录,显然失败了。当您从bash运行代码时,
~
将被展开,除非您要使用
shell=True
,命令将被传递到shell,shell将展开平铺,否则您需要使用
os.path.expanduser
或传递整个路径,即/home/user/other\u文件夹。。。。。。我会坚持使用shell=False搭配
os.path.expanduser(“~”
,您需要使用:

在Unix和Windows上,返回参数时,初始组件~or~ user被该用户的主目录替换


Python正在您的cwd中寻找一个名为
~
的目录,显然失败了。当您从bash运行代码时,
~
将被展开,除非您要使用
shell=True
,命令将被传递到shell,shell将展开平铺,否则您需要使用
os.path.expanduser
或传递整个路径,即/home/user/other\u文件夹。。。。。。我会坚持使用shell=False和
os.path.expanduser(“~”

尝试将
shell=True
添加到check\u输出命令中,但不要将
shell=True
与参数列表组合。尝试将
shell=True
添加到check\u输出命令中不要将
shell=True
与参数列表组合,尽管如此。注意:即使要添加
shell=True
,问题中的代码也不起作用,因为只有第一个列表项作为shell命令执行,其余的被解释为POSIX上的shell参数。换句话说:不要同时使用列表参数和
shell=True
,而是传递一个字符串,或者(更好)不要使用
shell=True
:如您所示,在这里调用
os.path.expanduser(“~/”)
。@J.F.Sebastian,
为什么要检查调用([“ls”,“~/”),shell=True)
那就开始工作吧?你了解
sh-c'cmd arg1'
(正确)和
sh-c'cmd'arg1
(错误)之间的区别吗?例如,请参见备注:即使要添加
shell=True
,问题中的代码也不会工作,因为只有第一个列表项作为shell命令执行,其余的被解释为POSIX上的shell参数。换句话说:不要同时使用列表参数和
shell=True
,而是传递一个字符串,或者(更好)不要使用
shell=True
:如您所示,在这里调用
os.path.expanduser(“~/”)
。@J.F.Sebastian,
为什么要检查调用([“ls”,“~/”),shell=True)
那就开始工作吧?你了解
sh-c'cmd arg1'
(正确)和
sh-c'cmd'arg1
(错误)之间的区别吗?例如,请参见
import os

os.path.expanduser('~/other_folder/src/models/sc_models.py')

In [2]: os.path.expanduser("~")
Out[2]: '/home/padraic'