Check If Path Exists Using Fabric

倾然丶 夕夏残阳落幕 提交于 2020-01-02 03:43:05

问题


I am running this code to check whether this directory exists on remote machine or not but this code is checking for the directory on local machine. How can I verify directory on remote machine?

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!"

回答1:


You can use the files.exists function.

def check_exists(filename):
    from fabric.contrib import files
    if files.exists(filename):
        print('%s exists!' % filename)

And call it with execute.

def main():
    execute(check_exists, '/path/to/file/on/remote')



回答2:


why not just keep it simply stupid as:

from fabric.contrib.files import exists

def foo():
    if exists('/path/to/remote/file', use_sudo=True):
      # do something...



回答3:


Although the accepted answer is valid for fabric ver 1, for whoever hits this thread while looking for the same thing but for fabric2:

exists method from fabric.contrib.files was moved to patchwork.files with a small signature change, so you can use it like this:

from fabric2 import Connection
from patchwork.files import exists

conn = Connection('host')
if exists(conn, SOME_REMOTE_DIR):
   do_something()


来源:https://stackoverflow.com/questions/44736637/check-if-path-exists-using-fabric

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!