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.