最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - How can I get the absolute path for a directory created with pyinfra? - Stack Overflow

programmeradmin1浏览0评论

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
Add a comment  | 

1 Answer 1

Reset to default 0

So 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.

发布评论

评论列表(0)

  1. 暂无评论