1、Python进行 sftp上传下载
import paramiko
if __name__ == '__main__':
sftpIp = 'x.x.x.x'
sftpPort = 22
sftpUsername = 'admin'
sftpPassword = '123456'
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)
sftp.get(remotePath, localPath)
sf.close()
2、sftp远程目录检测+创建
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