I am trying to make my little python script run by this command:
./pdweb_convert.py -i /path_to_file/input_file_name -o /path_to_file/output_file_name
This is my code:
import sys
inputfile = sys.argv[1]
outputfile = sys.argv[2]
with open(inputfile, 'r') as i, open(outputfile, 'w') as o:
o.seek(0)
o.truncate()
for line in i:
if '0x' in line:
new_line = line[9:50]
new_line = new_line.strip().replace(' ', '').replace('\n', '')
try:
new_line = bytearray.fromhex(new_line).decode("ascii")
except Exception:
new_line = ''
o.write(new_line)
else:
o.write(line)
If i try to run the program I get "IndexError: list index out of range" error on the line 'inputfile = sys.argv[1]'.
Any idea how to fix this and make my program work by typing the above command, where pdweb_convert is my script, the input file will be something like input.txt and output file any name since it will be created afterwards on my command.
Also how to make it print a warning message if the paths do not exist or the input file does not exist and as I said before, I want the output file to be automatically generated(created) using the name I give it in the command line, ex:
pdweb_convert.py -i C:\Users\Work\Documents\input.txt -o C:\Users\Work\Documents\outputexample.txt
How exactly can I code these inside my script, I started programming in Python this morning so I am ultra beginner. Thank you!