【实习日记】Sftp & Python上传本地多级文件夹

实现功能:将本地多级文件夹上传至sftp服务器

1.测试

由于公司的sftp已经投入使用了,没办法随便连接,先在本地用shutil库实现一个文件夹到另一个文件夹的复制,把一个本地文件夹当作远程服务器:

import os
import shutil

# 上传文件/多级文件夹
def upload(local_path, remote_path):

	# 没什么必要的一个判空
    if not os.path.exists(local_path):
        print('该路径不存在!' + local_path)

    # 格式化路径
    # '\'不影响识别路径找文件,但影响后续使用rfind()函数进行分割
    local_path = local_path.replace('\\', '/')
    remote_path = remote_path.replace('\\', '/')

    # 取本地文件夹路径最后一级
    ''' 如 'F:\\20221008', 则取'20221008'  '''
    index = local_path.rfind('/')

    # 合并远程文件夹和本地文件夹路径最后一级
    remote_path_name = os.path.join(remote_path, local_path[index + 1:])
    remote_path_name = remote_path_name.replace('\\', '/')

    # 在远程创建子文件夹
    os.mkdir(remote_path_name)

    # 遍历目录内容,上传文件
    for file in os.listdir(local_path):

        # 文件路径
        localfile = os.path.join(local_path, file)

        # 判断资源文件类型
        if os.path.isfile(localfile):
            ### 1.文件
            remotefile = os.path.join(remote_path_name, file)
            remotefile = remotefile.replace('\\', '/')
			# 复制文件
            shutil.copyfile(localfile, remotefile)
            localfile = localfile.replace('\\', '/')
            print('该文件复制成功' + localfile)

        elif os.path.isdir(localfile):
            ### 2.目录
            upload(localfile, remote_path_name)

if __name__ == "__main__":

    local_path = r'输入路径'
    remote_path = r'输入路径'
    upload(local_path, remote_path)

2.实现sftp上传多级文件夹

import os
import paramiko

# SSH连接类
class SSH(object):

    def __init__(self, host_dict):
        self.host = host_dict['host']
        self.port = host_dict['port']
        self.username = host_dict['username']
        self.pwd = host_dict['pwd']
        self.timeout = 30

        # 获取Transport实例
        self.t = paramiko.Transport(self.host, self.port)
        # 连接SSH服务端,使用password
        try:
            self.t.connect(username=self.username, password=self.pwd)
            print('ssh password connection succeeded!')
        except:
            print('ssh password connection failed!')

        # 获取SFTP实例
        self.sftp = paramiko.SFTPClient.from_transport(self.t)

    # 关闭通道
    def close(self):
        self.sftp.close()
        self.t.close()
        print('ssh connection closed!')
        self.sftp = None


    # 上传文件夹
    def upload(self, local_path, remote_path):

        if not os.path.exists(local_path):
            print('该路径不存在!' + local_path)

        # 格式化路径
        local_path = local_path.replace('\\', '/')
        remote_path = remote_path.replace('\\', '/')

        # 取本地文件夹路径最后一级
        ''' 如 'F:\\20221008', 则取'20221008'  '''
        index = local_path.rfind('/')

        # 合并远程文件夹和本地文件夹路径最后一级
        remote_path_name = os.path.join(remote_path, local_path[index + 1:])
        remote_path_name = remote_path_name.replace('\\', '/')

        # 在远程创建子文件夹
        self.sftp.mkdir(remote_path_name)

        # 遍历目录内容,上传文件
        for file in os.listdir(local_path):

            # 文件路径
            localfile = os.path.join(local_path, file)

            # 判断资源文件类型
            if os.path.isfile(localfile):
                ### 1.文件
                remotefile = os.path.join(remote_path_name, file)
                remotefile = remotefile.replace('\\', '/')

                self.sftp.put(localfile, remotefile)
                localfile = localfile.replace('\\', '/')
                print('该文件上传成功' + localfile )

            elif os.path.isdir(localfile):
                ### 2.目录
                self.upload(localfile, remote_path_name)


if __name__ == "__main__":
    host_dict = {
        'host': '输入ip',
        'port': 22,
        'username': '输入用户名',
        'pwd': '输入'

    }
    ssh = SSH(host_dict)

    local_path = r'输入路径'
    remote_path = r'输入路径'

    ssh.upload(local_path, remote_path)

    ssh.close()

实现效果:
	local_path = r'F:\20221008'
    remote_path = r'F:\python\Sftp_files'

运行前:
remote_path内为空,local_path内有多级文件夹及文件:
在这里插入图片描述

运行后:
remote_path内复制成功
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值