Obtain the file list from logs processed by Python

Source: Internet
Author: User
To prevent the log from being too large, every hour will re-write the log file, and the files between is renamed to the server_errors.log.yyyy-dd-mm-nn, where nn is the serial number, from 00 to 23.

Given the basic form of log, we need to analyze not only errors. log, but also other files generated on the current day. This requires a method to obtain the list of these files.

At the beginning, you are going to use the glob module to obtain the file list. glob generates a list of file names that meet this mode based on the given mode, which is the same as the Unix shell.
For eachfile in glob. glob (r "D: \ Log \ *. log "):
Print eachfile
Note that glob returns the complete path name, which is different from the OS. listdir function.

But glob is not a regular expression and is a UnixShell rule. Except *,?, [], Other regular expressions cannot be used.

Therefore, we can only set up a new stove. This time we use OS. listdir,
Because these files all start with the same name, you can use the startswith Method for filtering, and then obtain the list of log files generated today through the file creation time.

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.