Santosh Kumar G
Santosh Kumar G

Reputation: 5251

How to open a file by using a regular expression in python

I need to search for a pattern like (Error, Warning, Severity) in a file, This file will be located in a particular folder (e.g. D:\express\abc_xyz_9.0_12_9_17_23_11.log), the problem here is every time, when i install the software, the file will be generated based on the date and time of the system, so i can't hard code the file name, I need to write a regular expression to find the filename and open it and search for a pattern, here is the code, I have tried, but it throws an error as

TypeError: expected str, bytes or os.PathLike object, not list

Please find below to check for my code,

import os

path = os.listdir("D:\Express")
print(path)
with open(path) as f:
    for line in f:
        if python in f:
            print (line)

Upvotes: 0

Views: 10457

Answers (3)

Joe Iddon
Joe Iddon

Reputation: 20434

os.listdir() returns a list...

When using open() to open a file, you need to supply a path to a file - not a list. This is what is causing your error.

If I understand correctly, what you want is to loop through the files in that directory (getting them with os.listdir()) and for each file check if any line contains 'Error', 'Warning' or 'Severity'.

To achieve this, you could do something like if the file is in the current working directory:

import os

for file in os.listdir():
   with open(file) as f:
      for line in f:
         if 'Error' in line or 'Warning' in line or 'Severity in line:
            print('the line was', line)
            print('the file was', file)

The code above is quite self-explanatory, essentially, you loop through each file in the directory and open that file as 'f'. Then, for each line in the file 'f', you simply check if 'Error', 'warning' or 'Severity' is in that line.

In the code here, its just simply printing the line and containing file, but of course, if you wanted to store these or do something else with them you could.

Update:

I think the error is coming from the fact the file is not in the current working directory so you are looping through the file names in the directory and not the paths to the files. So when you try and do open(file), it does not work as it searches in the current directory that you are running the program in for just the filename rather than opens from the file path "D:\express\abc_xyz_9.0_12_9_17_23_11.log".

To solve this, you could do use os.path.join:

for file in os.listdir("D:\express"):
    with open(os.path.join("D:\express", "file")) as f:
       ...

or more simply just change the current working directory with os.chdir to "D:\express":

import os

os.chdir("D:\express")
for file in os.listdir():
   ...

personally I think changing the directory is neater.

Hope this helps!

Upvotes: 2

campovski
campovski

Reputation: 3163

You have to iterate over files in os.listdir(). You can do it like this:

import os

files = os.listdir('D:\express')
for file in files:
    f = open(os.path.join('D:\express\\', file))
    for line in f:
        if 'Error' in line or 'Warning' in line or 'Severity' in line:
            print(line)
    f.close()

Upvotes: 0

Aaron Paul
Aaron Paul

Reputation: 117

This error is caused because you are trying to test if python ( which doesn't seem to be defined anywhere) is in a list of files in the directory D:\Express. os.listdir() returns a list. If you wanted to print out the items in that list where "python" was in the filename, you would could try this:

directory_list = os.listdir("D:\Express")
for filename in directory_list:
    if "python" in filename:
        print (filename)

Your question is about trying to use a regular expression for this, but I don't think that's required for this simple search.

BTW. I usually use glob.glob for what you are talking about https://docs.python.org/2/library/glob.html

Upvotes: 0

Related Questions