Using grep in python
pythongrepos-systemshelltext-search
Abstraction: Running grep from Python via os.system with a readline loop
Key points:
- Uses
os.system()to execute grep commands built by string formatting:cmd = 'grep %s file.txt' % term - Reads search terms from a file line-by-line with
readline()inside a while loop - Common pitfall:
readline()retains the trailing newline character, breaking the shell command string - Pattern works for simple one-off searches but
subprocessmodule is preferred for capturing output
Connections: Python Scripting ยท Shell Integration
Source: http://stackoverflow.com/questions/9018109/using-grep-in-python