Reputation: 2172
I am trying to grep through a file using os.system("grep 'regex' /path/to/file.log")
. I know the regex is correct, and the command works when run from the shell, but once I try running the script I get the following error:
sh: /path/to/file.log: Permission denied
Is the os module not running with my permissions? Also, is there a better way to do this? I need to find specific lines in a 40,000+ line file.
THANKS!
Upvotes: 1
Views: 4348
Reputation: 612934
It's probably easier to do this using Python's built in regex module, re
, rather than shelling out to grep
.
import re
with open(filename, 'r') as f:
for line in f:
if re.search(regex, line):
print line,
I know this doesn't answer the question directly but it's probably the right solution to the underlying problem. I think this falls into the "is there a better way to do this?" part of your question.
Upvotes: 3