Wednesday, August 3, 2011

Python: Reading and Running Commands From A File

So I recently came across a broken process that needed some manual re-running. I was able to search through the log files and then see what part needed to be re-run. I ended up with a new data file (that I modified) to be a list of each command that failed for each iteration. So, the file looked something like this:
/path/to/program -option x -site a
/path/to/program -option x -site b
...
/path/to/program -option x -site z



Using a bash for line in `cat command.file`; do $line; done would not work since I would end up with a
-bash: -option: command not found
-bash: x: command not found
-bash: -site: command not found
error. So, here's some quick Python that can get the job done:

import os

f = open('/tmp/tmp-fix.commands')
for line in f:
    print line.rstrip()
    os.system(line.rstrip())


Yay for saving me some time! Cheers!

No comments: