使用 fabric.operations.put() 创建远程目录

Roy*_*ith 5 python sftp fabric

我需要将一些文件放到远程 sftp 服务器上,创建一个新目录来放置它们。有没有办法使用结构来做到这一点?Fabric.operations.put() 看起来不能在远程端创建新目录。

ale*_*cxe 8

mkdir在调用之前运行put()

run('mkdir -p /path/to/dir/')

put('/local/path/to/myfile', '/path/to/dir/')
Run Code Online (Sandbox Code Playgroud)

-p标志处理创建嵌套目录,请参阅:

-p,--父母

如果存在没有错误,请根据需要制作父目录


更新(仅用于 sftp 访问)。

使用SFTP.mkdir()

from contextlib import closing
from fabric.sftp import SFTP

ftp = SFTP(env.host_string)
with closing(ftp) as ftp:
    ftp.mkdir('/path/to/dir/', use_sudo=False)

put('/local/path/to/myfile', '/path/to/dir/')
Run Code Online (Sandbox Code Playgroud)