Python implements the blacklist of downloaded file records,

Source: Internet
Author: User
Tags ftp file

Python implements the blacklist of downloaded file records,

The Code is as follows:

#! /Usr/bin/python #-*-coding: GBK-*-#-*-coding: UTF-8-*-from ftplib import FTPimport osimport datetimeftp_server = '100. 0.0.1 '# username = 'ponshine' # username password = '1qaz2wsx' # password ftp_path ='/GBCC/'# ftp directory local_path = "C: \ F \ python \ pythonwangtest \ wyjj2 \ "# local directory # connect to FTPdef ftpconnect (): ftp = FTP () ftp. set_debuglevel (2) # enable debug level 2 and display the ftp details. connect (ftp_server, 21) # connect to ftp. login (username, password) # log on. If you log on anonymously, use an empty string to return ftp # obtain the current year, month, day, hour, minute, second def getdatetime (): I = datetime. datetime. now () date = ("% s" % (I. year, I. month, I. day, I. hour, I. minute, I. second) return date # The value must be returned. # obtain the current year, month, and day def getdate (): import datetime I = datetime. datetime. now () date = ("% s" % (I. year, I. month, I. day) return date # def downloadfile (remotepath, localpath): ftp = ftpconnect () # connect to ftp print ftp. getwelcome () # display ftp server welcome information ftp_filename = ftp. nlst (remotepath) # Use nlst () to obtain the file name 'ftp _ filename: ', ftp_filename # ftp file name for eachfile in ftp_filename: # obtain the file name if eachfile. endswith ('. AVL '): localpath_files = eachfile. split ("/") localpath_file = localpath_files [len (localpath_files)-1] # File Name: localpath_file = GBCC_201611102155_01.AVL print "localpath_file --->" + localpath_file # create the file name of the downloaded file name writefiletext = local_path + getdate () + ". txt "# record the downloaded file name 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") # open the blacklist table print "writefiletext --->" + writefiletext try: all_the_text = files. read () print "all_the_text -------" + all_the_text if all_the_text. _ contains _ (localpath_file): print "the file has been downloaded. You do not need to download the" else: print "file again, download "bufsize = 1024 # Set the buffer block size fp = open (localpath + localpath_file," wb + ") ftp. retrbinary ('retr '+ eachfile, fp. write, bufsize) # download file fo = open (writefiletext, "AB +") fo. write (localpath_file + "\ n") # write each file name to the file fo. flush () # refresh the file fo. close () fp. flush () finally: print "ended" files. close () ftp. set_debuglevel (0) ftp. close () if _ name _ = "_ main _": downloadfile ("/GBCC", "C: \ F \ python \ pythonwangtest \ wyjj2 \\")

Supplement: python blacklist Filtering

Filter words

Write the following function to add words to be filtered in the blacklist file.

# Filter 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

It is called through the filter function provided by python. in_lists and filter functions filter out the elements in the list with a bool value of 1.

 filter( in_lists , urls )

Summary

The above is the python download file record blacklist introduced by the editor. I hope it will be helpful to you. If you have any questions, please leave a message and the editor will reply to you in time. Thank you very much for your support for the help House website!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.