实现功能:将本地多级文件夹上传至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内复制成功