Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
python: Run specific command on file content line by line
#
# Run specific command on file content line by line
# Coded by Myo Soe
#
#
import sys, getopt, os
def main(argv):
#print len(argv)
if len(argv) is not 4:
print 'process-file.py -i <inputfile> -c os_command_to_run'
sys.exit()
inputfile = ''
command = 'echo '
try:
opts, args = getopt.getopt(argv, "hi:c:", ["ifile=", "cmd="])
except getopt.GetoptError:
print 'process-file.py -i <inputfile> -c <os_command>'
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print 'process-file.py -i <inputfile> -c os_command_to_run'
sys.exit()
elif opt in ("-i", "--ifile"):
inputfile = arg
elif opt in ("-c", "--cmd"):
command = arg
if os.path.exists(inputfile) == False:
print 'Input file "' + inputfile + '" was not found!'
#print 'Input file is "', inputfile
#print 'Command is "', command
f = open(inputfile)
for line in iter(f):
line = line.rstrip()
r = os.system(command + ' "' + line + '"');
#print r
f.close()
if __name__ == "__main__":
main(sys.argv[1:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment