
Mimi R.
asked 03/10/22Using regex to find file location from traceback file
so there’s a trace back log txt file that contains
- The assignment is to apply the function to return The file path and the line number
I tried using the same method as I used in log.txt but it didn’t work
I tried using re.findall(r’\/.*?\.[\w:]+)’,file)
1 Expert Answer
Chara M. answered 04/09/22
Hands-on Programming and Math tutor
I think the problem is your ending parenthesis. When I removed that, the regex worked fine: https://regex101.com/r/SDwDc0/1
I do recommend using the site above, or one similar to it, to verify that your expression is doing exactly what you want it to do. I've been wondering, would something like r'\/.+\.py' not be enough to match the filename, or does that match too much in some of your use cases?
You could capture the line number in the same regex by using matching groups. Then the result will be something like [('/home/datascience/logs/read_data.py', '1')], so the filepath will be at [0][0] and the line number will be at [0][1].
Good luck!
Still looking for help? Get the right answer, fast.
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
Ahmed K.
03/19/22