在 Python 中使用 SFTP 上传文件,但如果路径不存在则创建目录 [英] Upload files using SFTP in Python, but create directories if path doesn't exist

查看:199
本文介绍了在 Python 中使用 SFTP 上传文件,但如果路径不存在则创建目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用 Python 在远程服务器上上传文件.我想事先检查远程路径是否真的存在,如果不存在,则创建它.在伪代码中:

I want to upload a file on a remote server with Python. I'd like to check beforehand if the remote path is really existing, and if it isn't, to create it. In pseudocode:

if(remote_path not exist):
    create_path(remote_path)
upload_file(local_file, remote_path)

我正在考虑在 Paramiko 中执行一个命令来创建路径(例如 mkdir -p remote_path).我想出了这个:

I was thinking about executing a command in Paramiko to create the path (e.g. mkdir -p remote_path). I came up with this:

# I didn't test this code

import paramiko, sys

ssh = paramiko.SSHClient()
ssh.connect(myhost, 22, myusername, mypassword)
ssh.exec_command('mkdir -p ' + remote_path)
ssh.close

transport = paramiko.Transport((myhost, 22))
transport.connect(username = myusername, password = mypassword)

sftp = paramiko.SFTPClient.from_transport(transport)
sftp.put(local_path, remote_path)
sftp.close()

transport.close()

但是这个解决方案对我来说听起来不太好,因为我关闭了连接然后再次重新打开它.有没有更好的方法?

But this solution doesn't sound good to me, because I close the connection and then reopen it again. Is there a better way to do it?

推荐答案

SFTP 支持常用的 FTP 命令(chdir、mkdir 等...),因此请使用它们:

SFTP supports the usual FTP commands (chdir, mkdir, etc...), so use those:

sftp = paramiko.SFTPClient.from_transport(transport)
try:
    sftp.chdir(remote_path)  # Test if remote_path exists
except IOError:
    sftp.mkdir(remote_path)  # Create remote_path
    sftp.chdir(remote_path)
sftp.put(local_path, '.')    # At this point, you are in remote_path in either case
sftp.close()

<小时>

要完全模拟 mkdir -p,您可以递归地处理 remote_path:


To fully emulate mkdir -p, you can work through remote_path recursively:

import os.path

def mkdir_p(sftp, remote_directory):
    """Change to this directory, recursively making new folders if needed.
    Returns True if any folders were created."""
    if remote_directory == '/':
        # absolute path so change directory to root
        sftp.chdir('/')
        return
    if remote_directory == '':
        # top-level relative directory must exist
        return
    try:
        sftp.chdir(remote_directory) # sub-directory exists
    except IOError:
        dirname, basename = os.path.split(remote_directory.rstrip('/'))
        mkdir_p(sftp, dirname) # make parent directories
        sftp.mkdir(basename) # sub-directory missing, so created it
        sftp.chdir(basename)
        return True

sftp = paramiko.SFTPClient.from_transport(transport)
mkdir_p(sftp, remote_path) 
sftp.put(local_path, '.')    # At this point, you are in remote_path
sftp.close()

当然,如果remote_path也包含远程文件名,那么就需要将其拆分,将目录传递给mkdir_p,使用文件名代替'.'在 sftp.put 中.

Of course, if remote_path also contains a remote file name, then it needs to be split off, the directory being passed to mkdir_p and the filename used instead of '.' in sftp.put.

这篇关于在 Python 中使用 SFTP 上传文件,但如果路径不存在则创建目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆