使用 Fabric 检查路径是否存在
Check If Path Exists Using Fabric
我 运行 此代码用于检查此目录是否存在于远程计算机上,但此代码正在检查本地计算机上的目录。如何验证远程机器上的目录?
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!"
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')
为什么不只是 keep it simply stupid
作为:
from fabric.contrib.files import exists
def foo():
if exists('/path/to/remote/file', use_sudo=True):
# do something...
虽然接受的答案对 fabric ver 1 有效,但对于在寻找相同内容时点击此线程的任何人,但对于 fabric2:
exists
方法从 fabric.contrib.files
移动到 patchwork.files
有一个小的签名更改,所以你可以像这样使用它:
from fabric2 import Connection
from patchwork.files import exists
conn = Connection('host')
if exists(conn, SOME_REMOTE_DIR):
do_something()
我 运行 此代码用于检查此目录是否存在于远程计算机上,但此代码正在检查本地计算机上的目录。如何验证远程机器上的目录?
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!"
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')
为什么不只是 keep it simply stupid
作为:
from fabric.contrib.files import exists
def foo():
if exists('/path/to/remote/file', use_sudo=True):
# do something...
虽然接受的答案对 fabric ver 1 有效,但对于在寻找相同内容时点击此线程的任何人,但对于 fabric2:
exists
方法从 fabric.contrib.files
移动到 patchwork.files
有一个小的签名更改,所以你可以像这样使用它:
from fabric2 import Connection
from patchwork.files import exists
conn = Connection('host')
if exists(conn, SOME_REMOTE_DIR):
do_something()