python下载文件记录黑名单的实现代码


Posted in Python onOctober 24, 2017

具体代码如下所示:

#!/usr/bin/python
# -*- coding: GBK -*-
# -*- coding: UTF-8 -*-
from ftplib import FTP
import os
import datetime
ftp_server = '127.0.0.1' # 对应ftp服务器地址
username = 'ponshine' # 用户名
password = '1qaz2wsx' # 密码
ftp_path = '/GBCC/' # ftp目录
local_path = "C:\F\python\pythonwangtest\wyjj2\\" #本地的目录
# 连接FTP
def ftpconnect():
  ftp = FTP()
  ftp.set_debuglevel(2) # 打开调试级别2,显示详细信息
  ftp.connect(ftp_server, 21) # 连接
  ftp.login(username, password) # 登录,如果匿名登录则用空串代替即可
  return ftp
# 获取当前的年月日时分秒
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 # 需返回才能取值
# 获取当前的年月日
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服务器欢迎信息
  ftp_filename = ftp.nlst(remotepath) # 运用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
      # 创建记录下载文件名的文件名
      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 "文件已下载,不需要重复下载"
        else:
          print "文件没有下载,现在开始下载"
          bufsize = 1024 # 设置缓冲块大小
          fp = open(localpath + localpath_file, "wb+")
          ftp.retrbinary('RETR ' + eachfile, fp.write, bufsize) # 下载文件
          fo = open(writefiletext,"ab+")
          fo.write(localpath_file + "\n") # 将每个文件名写入文件
          fo.flush() # 刷新文件
          fo.close()
          fp.flush()
      finally:
        print "结束了"
        files.close()
  ftp.set_debuglevel(0)
  ftp.close()
if __name__ == "__main__":
  downloadfile("/GBCC", "C:\F\python\pythonwangtest\wyjj2\\")

  补充:python 黑名单过滤

  需要过滤一些词语

写了下面这个函数,在blacklist 文件中添加需要过滤的词语

#过滤黑名单列表中出现的
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函数 调用, in_lists ,filter函数会过滤掉 bool 值为 1 的列表中的元素

 filter( in_lists , urls )

总结

以上所述是小编给大家介绍的python下载文件记录黑名单,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对三水点靠木网站的支持!

Python 相关文章推荐
简单的抓取淘宝图片的Python爬虫
Dec 25 Python
Python获取邮件地址的方法
Jul 10 Python
Python和C/C++交互的几种方法总结
May 11 Python
python里使用正则表达式的组嵌套实例详解
Oct 24 Python
Python装饰器(decorator)定义与用法详解
Feb 09 Python
Ubuntu下Anaconda和Pycharm配置方法详解
Jun 14 Python
使用python将多个excel文件合并到同一个文件的方法
Jul 09 Python
django迁移数据库错误问题解决
Jul 29 Python
浅谈pytorch 模型 .pt, .pth, .pkl的区别及模型保存方式
May 25 Python
详解Python IO编程
Jul 24 Python
Python绘制组合图的示例
Sep 18 Python
浅析python 通⽤爬⾍和聚焦爬⾍
Sep 28 Python
基于python中staticmethod和classmethod的区别(详解)
Oct 24 #Python
Flask数据库迁移简单介绍
Oct 24 #Python
python里使用正则表达式的组嵌套实例详解
Oct 24 #Python
Scrapy的简单使用教程
Oct 24 #Python
详解python里使用正则表达式的分组命名方式
Oct 24 #Python
在python中使用正则表达式查找可嵌套字符串组
Oct 24 #Python
python爬虫之BeautifulSoup 使用select方法详解
Oct 23 #Python
You might like
php cookie 详解使用实例
2016/11/03 PHP
[原创]php实现数组按拼音顺序排序的方法
2017/05/03 PHP
使用jQuery全局事件ajaxStart为特定请求实现提示效果的代码
2010/12/30 Javascript
javascript学习笔记(八) js内置对象
2012/06/19 Javascript
js克隆对象、数组的常用方法介绍
2013/09/26 Javascript
JQuery EasyUI 日期控件如何控制日期选择区间
2014/05/05 Javascript
jQuery函数map()和each()介绍及异同点分析
2014/11/08 Javascript
JavaScript中对象介绍
2014/12/31 Javascript
IE7浏览器窗口大小改变事件执行多次bug及IE6/IE7/IE8下resize问题
2015/08/21 Javascript
jQuery CSS3自定义美化Checkbox实现代码
2016/05/12 Javascript
浅谈如何实现easyui的datebox格式化
2016/06/12 Javascript
JavaScript函数中关于valueOf和toString的理解
2016/06/14 Javascript
js 获取经纬度的实现方法
2016/06/20 Javascript
关于JS 预解释的相关理解
2016/06/28 Javascript
javascript中数组和字符串的方法对比
2016/07/20 Javascript
js手机号批量滚动抽奖实现代码
2020/04/17 Javascript
如何理解jQuery中的ajaxSubmit方法
2017/03/13 Javascript
vue-better-scroll 的使用实例代码详解
2018/12/03 Javascript
微信小程序收货地址API兼容低版本解决方法
2019/05/18 Javascript
JavaScript实现动态留言板
2020/03/16 Javascript
Element Steps步骤条的使用方法
2020/07/26 Javascript
[02:17]TI4西雅图DOTA2前线报道 啸天mik夫妻档解说
2014/07/08 DOTA
python pyenv多版本管理工具的使用
2019/12/23 Python
python3将变量写入SQL语句的实现方式
2020/03/02 Python
django admin 根据choice字段选择的不同来显示不同的页面方式
2020/05/13 Python
CSS中垂直居中的简单实现方法
2015/07/06 HTML / CSS
你可能不熟练的十个前端HTML5经典面试题
2018/07/03 HTML / CSS
HTML5中Canvas与SVG的画图原理比较
2013/01/16 HTML / CSS
优良学风班总结材料
2014/02/08 职场文书
给校长的建议书200字
2014/05/16 职场文书
幼儿园大班开学寄语
2014/08/02 职场文书
财务工作犯错检讨书
2014/10/07 职场文书
确保工程质量承诺书
2015/04/29 职场文书
pytest配置文件pytest.ini的详细使用
2021/04/17 Python
Java练习之潜艇小游戏的实现
2022/03/16 Java/Android
win10电脑关机快捷键是哪个 win10快速关机的几种方法
2022/08/14 数码科技