python下載文件記錄黑名單的實(shí)現(xiàn)代碼
具體代碼如下所示:
#!/usr/bin/python
# -*- coding: GBK -*-
# -*- coding: UTF-8 -*-
from ftplib import FTP
import os
import datetime
ftp_server = '127.0.0.1' # 對應(yīng)ftp服務(wù)器地址
username = 'ponshine' # 用戶名
password = '1qaz2wsx' # 密碼
ftp_path = '/GBCC/' # ftp目錄
local_path = "C:\F\python\pythonwangtest\wyjj2\\" #本地的目錄
# 連接FTP
def ftpconnect():
ftp = FTP()
ftp.set_debuglevel(2) # 打開調(diào)試級別2,顯示詳細(xì)信息
ftp.connect(ftp_server, 21) # 連接
ftp.login(username, password) # 登錄,如果匿名登錄則用空串代替即可
return ftp
# 獲取當(dāng)前的年月日時(shí)分秒
def getdatetime():
i = datetime.datetime.now()
date = ("%s%s%s%s%s%s" % (i.year, i.month, i.day, i.hour,i.minute,i.second))
return date # 需返回才能取值
# 獲取當(dāng)前的年月日
def getdate():
import datetime
i = datetime.datetime.now()
date = ("%s%s%s" % (i.year, i.month, i.day))
return date # 需返回才能取值
def downloadfile(remotepath, localpath):
ftp = ftpconnect() # 連接ftp
print ftp.getwelcome() # 顯示ftp服務(wù)器歡迎信息
ftp_filename = ftp.nlst(remotepath) # 運(yùn)用nlst()獲取文件名
print 'ftp_filename: ', ftp_filename # ftp上的文件名
for eachfile in ftp_filename: # 循壞取文件名
if eachfile.endswith('.AVL'):
localpath_files = eachfile.split("/")
localpath_file = localpath_files[len(localpath_files) - 1] # 文件名:localpath_file= GBCC_201611102155_01.AVL
print "localpath_file--->" + localpath_file
# 創(chuàng)建記錄下載文件名的文件名
writefiletext = local_path + getdate() + ".txt" # 記錄下載后的文件名
print "writefile_text--->" + writefiletext
if os.path.exists(writefiletext):
print writefiletext + "is exists"
else:
print writefiletext + "is not exists"
makefile = open(writefiletext,"w+")
makefile.close()
files = open(writefiletext, "r") # 打開黑名單表
print "writefiletext--->" + writefiletext
try:
all_the_text = files.read()
print "all_the_text-------》" + all_the_text
if all_the_text.__contains__(localpath_file):
print "文件已下載,不需要重復(fù)下載"
else:
print "文件沒有下載,現(xiàn)在開始下載"
bufsize = 1024 # 設(shè)置緩沖塊大小
fp = open(localpath + localpath_file, "wb+")
ftp.retrbinary('RETR ' + eachfile, fp.write, bufsize) # 下載文件
fo = open(writefiletext,"ab+")
fo.write(localpath_file + "\n") # 將每個(gè)文件名寫入文件
fo.flush() # 刷新文件
fo.close()
fp.flush()
finally:
print "結(jié)束了"
files.close()
ftp.set_debuglevel(0)
ftp.close()
if __name__ == "__main__":
downloadfile("/GBCC", "C:\F\python\pythonwangtest\wyjj2\\")
補(bǔ)充:python 黑名單過濾
需要過濾一些詞語
寫了下面這個(gè)函數(shù),在blacklist 文件中添加需要過濾的詞語
#過濾黑名單列表中出現(xiàn)的
def in_lists(str):
str_lists=[]
fd = open('./filter/blacklist')
for line in fd.readlines():
str_lists.append(line.strip())
if str in str_lists:
return 0
else:
return 1
通過 python 自帶的 filter函數(shù) 調(diào)用, in_lists ,filter函數(shù)會(huì)過濾掉 bool 值為 1 的列表中的元素
filter( in_lists , urls )
總結(jié)
以上所述是小編給大家介紹的python下載文件記錄黑名單,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
Python實(shí)現(xiàn)郵件自動(dòng)下載的示例詳解
python3中http協(xié)議提供文件服務(wù)器功能詳解
Python capitalize()函數(shù)的用法詳解
Python動(dòng)態(tài)參數(shù)/命名空間/函數(shù)嵌套/global和nonlocal
python實(shí)現(xiàn)sublime3的less編譯插件示例

