kra*_*r65 1 python bash subprocess ln exit-code
正如标题所说:
>>> 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
$
Run Code Online (Sandbox Code Playgroud)
怎么会这样?如何从命令行取得成功,但是从Python子进程调用失败了?
欢迎所有提示!
您需要使用os.path.expanduser:
在Unix和Windows上,返回带有〜或〜用户主目录替换的初始组件的参数.
import os
os.path.expanduser('~/other_folder/src/models/sc_models.py')
In [2]: os.path.expanduser("~")
Out[2]: '/home/padraic'
Run Code Online (Sandbox Code Playgroud)
Python正在寻找~
你的cwd中命名的目录,这个目录显然会失败.当您从bash运行代码时,~
会扩展,除非您使用shell=True
命令将传递给shell并且shell将扩展波形符,然后您需要使用os.path.expanduser
或传递整个路径,即/ home/user/other_folder ......我会坚持使用shell = False os.path.expanduser("~")
.
归档时间: |
|
查看次数: |
173 次 |
最近记录: |