Skip to content

Instantly share code, notes, and snippets.

@yehgdotnet
Created August 26, 2020 11:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yehgdotnet/72cb19831c0e4ee017450d84a932a1c8 to your computer and use it in GitHub Desktop.
Save yehgdotnet/72cb19831c0e4ee017450d84a932a1c8 to your computer and use it in GitHub Desktop.
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