首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Fabric检查路径是否存在

使用Fabric检查路径是否存在
EN

Stack Overflow用户
提问于 2017-06-24 20:58:25
回答 3查看 7.5K关注 0票数 7

我运行这段代码是为了检查远程机器上是否存在这个目录,但这段代码检查的是本地机器上的目录。如何验证远程计算机上的目录?

代码语言:javascript
复制
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!"
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-07-02 01:12:58

您可以使用files.exists function

代码语言:javascript
复制
def check_exists(filename):
    from fabric.contrib import files
    if files.exists(filename):
        print('%s exists!' % filename)

并使用execute调用它。

代码语言:javascript
复制
def main():
    execute(check_exists, '/path/to/file/on/remote')
票数 12
EN

Stack Overflow用户

发布于 2019-01-30 20:05:47

尽管公认的答案对于fabric版本1是有效的,但对于任何点击这个线程的人来说,除了fabric2之外,他们都在寻找相同的东西:

fabric.contrib.files中的exists方法被移到了patchwork.files中,只做了一个小的签名更改,所以您可以这样使用它:

代码语言:javascript
复制
from fabric2 import Connection
from patchwork.files import exists

conn = Connection('host')
if exists(conn, SOME_REMOTE_DIR):
   do_something()
票数 11
EN

Stack Overflow用户

发布于 2018-01-07 18:24:33

为什么不直接使用keep it simply stupid

代码语言:javascript
复制
from fabric.contrib.files import exists

def foo():
    if exists('/path/to/remote/file', use_sudo=True):
      # do something...
票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44736637

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档