好友
阅读权限10
听众
最后登录1970-1-1
|
from ftplib import FTP
import shutil
import os
import datetime
from ftplib import FTP
import re
def yearmonthday():
date01 = datetime.date.today()
date01 = str(date01)
year = date01[0:4]
month = date01[5:7]
day = date01[8:11]
monthday = month + day
filename = str(year) +"."+ str(month) +"."+ str(day)
return filename
def create_file(filename):
# 经常用到的(如果文件夹不存在,则创建该文件夹)
if not os.path.exists("F:" + filename):
os.makedirs("F:" + filename)
localdir= "F:" + filename
return localdir
def ftp_link():
ftp = FTP()
# 连接ftp
ftp.connect("", )
# ftp登录
ftp.login("", "")
# 查看欢迎信息
print(ftp.getwelcome())
return ftp
def ftp_download(LocalFile, RemoteFile, bufsize):
# 本地是否有此文件,来确认是否启用断点续传
if not os.path.exists(LocalFile):
with open(LocalFile, 'wb') as f:
ftp.retrbinary('RETR %s' % RemoteFile, f.write, bufsize)
f.close()
# ftp.set_debuglevel(0) #关闭调试模式
return True
else:
p = re.compile(r'\\',re.S)
LocalFile = p.sub('/', LocalFile)
localsize = os.path.getsize(LocalFile)
with open(LocalFile, 'ab+') as f:
ftp.retrbinary('RETR %s' % RemoteFile, f.write, bufsize, localsize)
f.close()
# ftp.set_debuglevel(0) #关闭调试模式
return True
def DownLoadFileTree(LocalDir, RemoteDir):
print("RemoteDir:", RemoteDir)
if not os.path.exists(LocalDir):
os.makedirs(LocalDir)
# 打开该远程目录
ftp.cwd(RemoteDir)
# 获取该目录下所有文件名,列表形式
RemoteNames = ftp.nlst()
print(RemoteNames)
for file in RemoteNames:
Local = os.path.join(LocalDir, file) # 下载到当地的全路径
print(ftp.nlst(file)) # [如test.txt]
if file.find(".") == -1: # 是否子目录 如test.txt就非子目录
if file.find("README") == -1:
if not os.path.exists(Local):
os.makedirs(Local)
DownLoadFileTree(Local, file) # 下载子目录路径
else:
ftp.voidcmd('TYPE I') # 将传输模式改为二进制模式 ,避免提示 ftplib.error_perm: 550 SIZE not allowed in ASCII
bufsize = ftp.size(file) # 服务器里的文件总大小
print(bufsize)
ftp_download(Local, file, bufsize)
else:
ftp.voidcmd('TYPE I') # 将传输模式改为二进制模式 ,避免提示 ftplib.error_perm: 550 SIZE not allowed in ASCII
bufsize = ftp.size(file) # 服务器里的文件总大小
print(bufsize)
ftp_download(Local, file, bufsize)
ftp.cwd("..") # 返回路径最外侧
return
filename = yearmonthday()
print (filename)
bh = input("请输入:")
year = bh[0:4]
monthday = bh[4:8]
RemoteDir =
localdir = create_file(filename)
localdir = localdir +'\\'+ bh
print(localdir)
ftp = ftp_link()
DownLoadFileTree(localdir, RemoteDir)
|
|
发帖前要善用【论坛搜索】功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。 |
|
|
|
|