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.
- fileList = []
- pattern = r"seeUthere_errors.log(\.\d{4}-\d{2}-\d{2}-\d{2})"
- for eachfile in glob.glob(r"D:\Log\./*"):
- if re.search(pattern, eachfile):
- 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:
- import time
- current = time.strftime("%Y-%m-%d", time.gmtime())
Then modify pattern:
- pattern = r"seeUthere_errors.log(\.)" + current + "(-\d{2})"
You can use the Python method above to obtain the file list.