ftp下载目录下的文件

#!/usr/bin/python
# -*- coding:utf-8 -*-
# coding=utf-8
import paramiko
import os
import time
datenow = time.strftime("%Y%m%d")
def DownLoadFile(sftp, LocalFile, RemoteFile): # 下载当个文件
file_handler = open(LocalFile, 'wb')
print(file_handler)
sftp.get(RemoteFile, LocalFile) # 下载目录中文件
file_handler.close()
return True


def DownLoadFileTree(sftp, LocalDir, RemoteDir): # 下载整个目录下的文件
if not os.path.exists(LocalDir):
os.makedirs(LocalDir)
for file in sftp.listdir(RemoteDir):
print(file)
Local = os.path.join(LocalDir, file)
Remote = os.path.join(RemoteDir, file)
if file.find(".") == -1 : # 判断是否是文件
if not os.path.exists(Local):
os.makedirs(Local)
DownLoadFileTree(sftp, Local, Remote)
else: # 文件
DownLoadFile(sftp, Local, Remote)
return "complete"



if __name__ == '__main__':
hostname = "183.**.***.***"
port = 22
username = "****"
password = "****"
sf = paramiko.Transport((hostname, port))
sf.connect(username=username, password=password)
sftp = paramiko.SFTPClient.from_transport(sf)

list=['cust','daily','opposite','sale']
for i in list:
remote = '/data/sftp/lszd/' + datenow+ "/" +i+ "/" # 目标文件所在路径
local = 'D:\\sftp\\'+i # 存储路径
print(local)
print(remote)
DownLoadFileTree(sftp,local,remote)#下载
posted @ 2020-06-10 22:35  小黑仔学It  阅读(470)  评论(0编辑  收藏  举报