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中的内存泄漏
Apr 23 Python
Python脚本实现Web漏洞扫描工具
Oct 25 Python
今天 平安夜 Python 送你一顶圣诞帽 @微信官方
Dec 25 Python
Python 实现引用其他.py文件中的类和类的方法
Apr 29 Python
Python实现的爬取网易动态评论操作示例
Jun 06 Python
解决python tkinter界面卡死的问题
Jul 17 Python
Python (Win)readline和tab补全的安装方法
Aug 27 Python
tensorflow 实现自定义layer并添加到计算图中
Feb 04 Python
pytorch对梯度进行可视化进行梯度检查教程
Feb 04 Python
Python关于__name__属性的含义和作用详解
Feb 19 Python
PyCharm 无法 import pandas 程序卡住的解决方式
Mar 09 Python
详解Python中openpyxl模块基本用法
Feb 23 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
使用session判断用户登录用户权限(超简单)
2013/06/08 PHP
session在php5.3中的变化 session_is_registered() is deprecated in
2013/11/12 PHP
PHP5多态性与动态绑定介绍
2015/04/03 PHP
windows平台中配置nginx+php环境
2015/12/06 PHP
php-beanstalkd消息队列类实例分享
2017/07/19 PHP
PHP设计模式之装饰器模式定义与用法详解
2018/04/02 PHP
Jquery Ajax 学习实例2 向页面发出请求 返回JSon格式数据
2010/03/15 Javascript
Js 刷新框架页的代码
2010/04/13 Javascript
奉献给JavaScript初学者的编写开发的七个细节
2011/01/11 Javascript
WebPack基础知识详解
2017/01/16 Javascript
JS获得一个对象的所有属性和方法实例
2017/02/21 Javascript
Bootstrap笔记—折叠实例代码
2017/03/13 Javascript
详解如何在 vue 项目里正确地引用 jquery 和 jquery-ui的插件
2017/06/01 jQuery
koa上传excel文件并解析的实现方法
2018/08/09 Javascript
微信小程序实现收货地址左滑删除
2020/11/18 Javascript
python计算最小优先级队列代码分享
2013/12/18 Python
Python中使用item()方法遍历字典的例子
2014/08/26 Python
python中实现php的var_dump函数功能
2015/01/21 Python
使用XML库的方式,实现RPC通信的方法(推荐)
2017/06/14 Python
机器学习的框架偏向于Python的13个原因
2017/12/07 Python
Python对象属性自动更新操作示例
2018/06/15 Python
使用Python进行体育竞技分析(预测球队成绩)
2019/05/16 Python
Python使用Beautiful Soup爬取豆瓣音乐排行榜过程解析
2019/08/15 Python
解决ROC曲线画出来只有一个点的问题
2020/02/28 Python
Selenium自动化测试工具使用方法汇总
2020/06/12 Python
Python爬虫之Selenium实现键盘事件
2020/12/04 Python
印尼网上商店:Alfacart.com
2019/03/11 全球购物
新西兰网上购物,折扣店:BestDeals.co.nz
2019/03/20 全球购物
C面试题
2015/10/08 面试题
物流专业求职计划书
2014/01/10 职场文书
新郎答谢词
2015/01/04 职场文书
给老婆道歉的话
2015/01/20 职场文书
个人收入证明范本
2015/06/12 职场文书
关于flex 上下文中自动 margin的问题(完整例子)
2021/05/20 HTML / CSS
Python 的 sum() Pythonic 的求和方法详细
2021/10/16 Python
CentOS8.4安装Redis6.2.6的详细过程
2021/11/20 Redis