python - 使用 Fabric 检查路径是否存在

标签 python ubuntu 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!"

最佳答案

虽然接受的答案对 fabric ver 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()

关于python - 使用 Fabric 检查路径是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44736637/

相关文章:

python - 时间复杂度: growing list in nested for-loops

bash - echo -e 在 ubuntu 上由 root 在脚本中运行时表现不同

ubuntu - 在 Ubuntu 14.04 上安装 Meteor 时遇到问题

python - 如何使用 Fabric 使用 nohup 启动后台进程?

python - 对列表列表中分组的多个列表应用不同的权重 - Python

python - TTK,按钮上只有一个数字

bash - 我如何从 BASH 中的变量追加或覆盖?

python - 如何通过 Fabric 设置远程服务器时区?

python - 如何中止 python Fabric run 命令?

python - 禁用特殊的 "class"属性处理