admin管理员组文章数量:1319953
I created a new directory with pyinfra using the relative path new_dir
from pyinfra.operations import files, server
files.directory("new_dir")
Now I would like to get the absolute path of the new directory. I tried
result = server.shell("pwd", _chdir="new_dir")
print(result.stdout)
But that only raises an exception:
RuntimeError: Cannot evaluate operation result before execution
How can I get the absolute path of the directory pyinfra created for me?
I created a new directory with pyinfra using the relative path new_dir
from pyinfra.operations import files, server
files.directory("new_dir")
Now I would like to get the absolute path of the new directory. I tried
result = server.shell("pwd", _chdir="new_dir")
print(result.stdout)
But that only raises an exception:
RuntimeError: Cannot evaluate operation result before execution
How can I get the absolute path of the directory pyinfra created for me?
Share Improve this question asked Jan 19 at 21:01 asmaierasmaier 11.8k11 gold badges85 silver badges106 bronze badges 1- Check if the Operation Output section in the documentation helps. – Man made of meat Commented Jan 20 at 0:40
1 Answer
Reset to default 0So as @user202311 suggested I checked the documentation and it suggested to try a nested operation. So
from pyinfra.operations import files, server, python
DIR_NAME = "new_dir"
files.directory(DIR_NAME)
python.call(lambda: print(server.shell("pwd", _chdir=DIR_NAME).stdout))
will in fact print out the absolute path of the created directory.
However I was not able to extract the value out of the callback function, so that I can use it in the following steps. The only way to do what I wanted was to guess the absolute path differently like
from pyinfra import host
from pyinfra.operations import files, server
home = host.get_fact(server.Home, "")
DIR_NAME = "new_dir"
files.directory(DIR_NAME)
path = home + "/" + DIR_NAME
print(path)
But this will only work if your working directory was in fact the home directory.
本文标签: pythonHow can I get the absolute path for a directory created with pyinfraStack Overflow
版权声明:本文标题:python - How can I get the absolute path for a directory created with pyinfra? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742059276a2418482.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论