1

I need to scan a directory for ex: C:\Users\Jack\Work and search for a file name which has part text part numbers ex: Worklog_201810716_081.log.
Could anyone please help me how can I use regexp in my code to search the file name specifically.

I have implemented the following hard code with the file name:

reg_lst = ["Error in log"]
for i, line in enumerate(open("C:\\Users\\Jack\Work\\Worklog_201810716_081.log")):
    if any(compiled_reg.match(line) for compiled_reg in reg_lst):
        print("Found on line %s" % (i+1))
        print("Log msg: ", line)

This prints the message after Error in log in the Worklog_201810716_081.log file.

I need to write a generic code where I need to also scan other log files in the directory for a text search.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
crazybyPy
  • 31
  • 6
  • I don't understand what your question is. It seems that this code is doing what you want it do do, or not? Please explain. – mkrieger1 Jun 20 '18 at 08:08
  • This code specifically searches in the Worklog_201810716_081.log file for text. Along with Worklog_201810716_081.log file I have other log files (like for Ex: Worklog_201810714_078.log , Worklog_201810516_096.log and so on). I need to scan the directory for all such log files and the text inside them – crazybyPy Jun 20 '18 at 08:11
  • Are different files on different lines in that file? – Melvin Abraham Jun 20 '18 at 08:13
  • Did you try one of the things shown in this question? https://stackoverflow.com/questions/2225564/get-a-filtered-list-of-files-in-a-directory – mkrieger1 Jun 20 '18 at 08:14

2 Answers2

0

use glob.glob or os.scandir or fnmatch.fnmatch.

Try glob(r'C:\Users\Jack\Work\*.log') in the package glob. This should show a list of filename of .log files under the directory C:\Users\Jack\Work.

Untested code:

from glob import glob

reg_lst = ["Error in log"]

for filename in glob(r'C:\Users\Jack\Work\*.log'):
    with open(filename, 'r') as f:
        for i, line in enumerate(f.readlines()):
            if any(compiled_reg.match(line) for compiled_reg in reg_lst):
              print("Found on line %s" % (i+1))
              print("Log msg: ", line)

Another disscusion about filtering file by name.

Aaron
  • 1,255
  • 1
  • 9
  • 12
  • btw, the filename filtering does not use `regex`. it is called `Unix shell-style wildcard` or `fnmatch` as mentioned on https://docs.python.org/3.6/library/fnmatch.html#module-fnmatch – Aaron Jun 20 '18 at 08:26
0

I was able to write the following code and it works successfully.

reg_lst = ["Error in log"]
work_path = "C:\Users\Jack\Work\"

for file in os.listdir(work_path):
  if fnmatch.fnmatch(file, '*.log'):
    for i, line in enumerate(open(os.path.join(work_path,file))):
        if any(compiled_reg.match(line) for compiled_reg in reg_lst):
            print("Found on line %s" % (i+1))
            print("Log msg: ", line)

It searches all the log files in the work_path directory and searches the text "Error in log" and prints the line number and the entire text message on that line.

crazybyPy
  • 31
  • 6