pythonubuntufabric

Check If Path Exists Using Fabric


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

Solution

  • 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')