1、Python进行 sftp上传下载
# 下载SFTP代码内容 pip install paramiko
import paramiko
if __name__ == '__main__':
sftpIp = 'x.x.x.x' # sftpIP地址
sftpPort = 22 # sftp端口,默认是22
sftpUsername = 'admin' # sftp登录名称
sftpPassword = '123456' # sftp密码
localPath = 'C:/xxxx/Desktop/xxx.xlsx' # 本地全路径
remotePath = '/xxx/xxx/xxx.xlsx' # 远程全路径,必须存在
#初始化 + 连接
sf = paramiko.Transport((sftpIp, sftpPort))
sf.connect(username=sftpUsername, password=sftpPassword)
sftp = paramiko.SFTPClient.from_transport(sf)
# put 上传 get 下载
# sftp.put(localPath, remotePath)
sftp.get(remotePath, localPath)
# 关闭连接
sf.close()
2、sftp远程目录检测+创建
# 与1结合
def createSftpDir(sftp, remotePath):
index = 0
while True:
pathIndex = remotePath.index("/", index + 1)
if index <= pathIndex:
index = pathIndex
path = remotePath[0:pathIndex]
try:
sftp.stat(path)
pass
except Exception as e:
sftp.mkdir(path)
if pathIndex >= remotePath.rindex("/"):
break
3、python本地文件夹检测和创建
import os
# 创建本地的文件夹
def createLocalDir(loaclPath):
try:
if loaclPath.index("\\")>-1:
loaclPath = loaclPath.replace("\\", "/")
except Exception as e:
loaclPath
if not os.path.isdir(loaclPath):
createPath = loaclPath[0:loaclPath.rindex("/")]
if not os.path.exists(createPath):
os.makedirs(createPath)
return localPath
————————————————
原文链接:https://blog.csdn.net/u011008832/article/details/127557681