Using Python to implement regular matching retrieves files in the remote FTP directory _python

Source: Internet
Author: User
Tags ftp client

Encountered a problem, need to match the file in the remote FTP directory, if you can use an FTP client to do this easily by using the command line, but there is no tool to support such a requirement, then with Python support for FTP and support for regular expressions, write a simple tool A file that uses regular expressions to match a remote directory.

The code is as follows

# coding=utf-8 ######################################################################### # File Name:reg_url.py #

Author:wangweilong # Company:baidu ######################################################################### Import RE import sys import OS from ftplib import FTP dhccmd = "Http://xxx/api/submit" Class Dhc_ftp (): Def __init__ (SE LF, hostname, username= "", Passwd= ""): Self.hostname = hostname try:self.ftp = FTP (self.hostname) except:pri
   NT "hostname error!" Exit ( -1) Self.username = Username SELF.PASSWD = passwd self.filelist = [] Self.reg_pattern = "" Def Getftpfileli St (Self, Path): Self.ftp.login (Self.username, self.passwd) self.ftp.cwd (path) self.filelist = Self.ftp.nlst () def  Matchnames (self, regpattern): pattern = Re.compile (regpattern) matchedfiles = [] for file in Self.filelist:match = Pattern.search (file) if Match:matchedfiles.append (match.string) return matchedfiles if __name__ = "__main __ ": If Len (sys. argv)!= 4:print ' Usage:python reg_url.py $ftpurldir $regular ' else: #解析ftpurl字符串 head = Sys.argv[1].split ("@") [1 ].split ("/") [0] username = sys.argv[1].split ("//") [1].split (":") [0] passwd = sys.argv[1].split ("//") [1].split (":") [1 ].split ("@") [0] Pathdir = sys.argv[1].split (head) [1] dhc_ftp = dhc_ftp (head, username, passwd) Dhc_ftp.getftpfileli St ("./" + pathdir) #获取正则表达式 Regpattern = R '%s '% sys.argv[2] matchedfiles = Dhc_ftp.matchnames (Regpattern) for F
 Iles in matchedfiles:ftpname = sys.argv[1] + "/" + Files print Ftpname

By entering the remote directory address you want to retrieve and the regular expression you want to match, you can get to the far end of the file, as to what to do with these files, that is the demand problem.

The above mentioned is the entire content of this article, I hope you can enjoy.

Please take a moment to share the article with your friends or leave a comment. We will sincerely thank you for your support!

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.