我运行这段代码是为了检查远程机器上是否存在这个目录,但这段代码检查的是本地机器上的目录。如何验证远程计算机上的目录?
rom fabric.api import run, sudo, env
import os
env.hosts = ['remote_server']
env.user = 'ubuntu'
env.key_filename = '/home/ubuntu/ubuntu16-multi.pem'
def Directory_Check():
DIR_1="/home/ubuntu/test-dir"
if os.path.exists(DIR_1):
print "Directory Exist!"
else:
print "Directory Does Not Exist!"发布于 2017-07-02 01:12:58
您可以使用files.exists function。
def check_exists(filename):
from fabric.contrib import files
if files.exists(filename):
print('%s exists!' % filename)并使用execute调用它。
def main():
execute(check_exists, '/path/to/file/on/remote')发布于 2019-01-30 20:05:47
尽管公认的答案对于fabric版本1是有效的,但对于任何点击这个线程的人来说,除了fabric2之外,他们都在寻找相同的东西:
fabric.contrib.files中的exists方法被移到了patchwork.files中,只做了一个小的签名更改,所以您可以这样使用它:
from fabric2 import Connection
from patchwork.files import exists
conn = Connection('host')
if exists(conn, SOME_REMOTE_DIR):
do_something()发布于 2018-01-07 18:24:33
为什么不直接使用keep it simply stupid:
from fabric.contrib.files import exists
def foo():
if exists('/path/to/remote/file', use_sudo=True):
# do something...https://stackoverflow.com/questions/44736637
复制相似问题