質問

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!

役に立ちましたか?

解決

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top