1

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!

2 Answers 2

1
python ./pdweb_convert.py -i /path_to_file/input_file_name -o /path_to_file/output_file_name

(I am assuming you also wrote python at the beginning of this command.)

The issue is that your sys.argv returns a list of five elements and you are calling 1 and 2 index which are -i and /path_to_file/input_file_name. Instead these you should have called 2 and 4 indexed. When I ran your code it did not raise any list error but logically it errored

C:\Users\Desktop>python x.py -i /path_to_file/input_file_name -o /path_to_file/output_file_name
Traceback (most recent call last):
  File "x.py", line 4, in <module>
    with open(inputfile, 'r') as i, open(outputfile, 'w') as o:
FileNotFoundError: [Errno 2] No such file or directory: '-i'
Sign up to request clarification or add additional context in comments.

Comments

1

Python has a library called optparse that makes this easy

You can use it like so:

#!/usr/bin/python

import optparse

parser = optparse.OptionParser()
parser.add_option('-i', help='arguments', dest='infile', action='store')
parser.add_option('-o', help='arguments', dest='outfile', action='store')
(opts, args) = parser.parse_args()
print(opts)

Output:

{'infile': 'infile.txt', 'outfile': 'outfile.txt'}

So you can just grab opts['infile'] and opts['outfile'] and check if those files exist on the file system.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.