Basic Application methods for obtaining file lists in Python

Source: Internet
Author: User
Tags glob

The powerful Python programming language plays an important role in the development field. Its advantages in log processing are also obvious. For example, this article describes how to use the glob module to obtain the file list in Python. However, due to the limited regular expressions that glob can use, it can only be said that some tasks have been completed, inspired by reading "Python technical reference book" today, we can use regular expressions to check each file name so that we can find the list of required files.

 
 
  1. fileList = []  
  2. pattern = r"seeUthere_errors.log(\.\d{4}-\d{2}-\d{2}-\d{2})" 
  3. for eachfile in glob.glob(r"D:\Log\./*"):  
  4. if re.search(pattern, eachfile):  
  5. fileList.append(eachfile) 

Extension of the Python method for obtaining the file list:

What if I want to obtain the log list of the composite Regular Expression generated on the current day?

Based on the log generation format, we can use the strftime method of the time module to obtain the current day:

 
 
  1. import time  
  2. current = time.strftime("%Y-%m-%d", time.gmtime()) 

Then modify pattern:

 
 
  1. pattern = r"seeUthere_errors.log(\.)" + current + "(-\d{2})" 

You can use the Python method above to obtain the file list.

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.