1

I am manually running file with command line

python script.py input_files/input.txt text_out/output.json

while inside script.py there is

input_path = sys.argv[1]
out_path = sys.argv[2]

now I have shell script and I want to make it for all files in one go. I am facing issue.

My shell script is like below there are two folders 1) input_files and 2) text_out

for i in input_files/*.txt;
  do name=`echo "$i" | cut -d'.' -f1`
  echo "$name"
  python script.py -i "$i" text_out/"${name}.json"
done

but when I execude .sh as stated above, it is throwing error as sys.argv is not picking properly.

out_path = sys.argv[2]
IndexError: list index out of range

If you can guide what to change in .py or in shell .sh script would be kind.

5
  • You forgot to pass the input file as the first argument to script.py. Commented Jun 4, 2021 at 12:32
  • I tried python script.py $i text_out/"${name}.json" but it didn't work Commented Jun 4, 2021 at 13:01
  • Run your shell script with -x to see what arguments are actually getting passed to python. Commented Jun 4, 2021 at 13:18
  • Thank you all and specially @kojiro and mportes. I finally resolved it. Below is my own answer and correction ! if you can accept it. Commented Jun 4, 2021 at 13:28
  • @ML85 only you can accept the answer to your own question. But please do take a look at my answer on argparse. :) Commented Jun 4, 2021 at 13:33

3 Answers 3

1

I don't know exactly why you're getting a ListIndexOutOfRange, but it doesn't really matter, since you're also passing -i after script.py, so out_path cannot be what you expect.

$ cat script.py 
import sys; print(len(sys.argv)); print(sys.argv); print({i:v for i, v in enumerate(sys.argv)})

$ (set -x; i=input_files/foo.txt; name=`echo "$i" | cut -d'.' -f1`; python script.py -i "$i" text_out/"${name}.json")
+ i=input_files/foo.txt
++ echo input_files/foo.txt
++ cut -d. -f1
+ name=input_files/foo
+ python script.py -i input_files/foo.txt text_out/input_files/foo.json
4
['script.py', '-i', 'input_files/foo.txt', 'text_out/input_files/foo.json']
{0: 'script.py', 1: '-i', 2: 'input_files/foo.txt', 3: 'text_out/input_files/foo.json'}

I recommend using argparse whenever you need to deal with cli arguments in Python. It will give better feedback and reduce the ambiguity of looking directly at indices.

$ cat script2.py 
import argparse

parser = argparse.ArgumentParser()
parser.add_argument('-i', '--input-file', type=argparse.FileType('r'))
parser.add_argument('output_file', type=argparse.FileType('w'))
print(parser.parse_args())

$ (set -x; i=input_files/foo.txt; name=`echo "$i" | cut -d'.' -f1`; python script2.py -i "$i" text_out/"${name}.json")
+ i=input_files/foo.txt
++ echo input_files/foo.txt
++ cut -d. -f1
+ name=input_files/foo
+ python script2.py -i input_files/foo.txt text_out/input_files/foo.json
Namespace(input_file=<_io.TextIOWrapper name='input_files/foo.txt' mode='r' encoding='UTF-8'>, output_file=<_io.TextIOWrapper name='text_out/input_files/foo.json' mode='w' encoding='UTF-8'>)
Sign up to request clarification or add additional context in comments.

Comments

0
for i in input_files/*.txt;
  do name=`echo "$i" | cut -d'.' -f1`
  echo "$name"
  python script.py "$i" text_out/"${name}.json"
done

Comments

0

You get the IndexError: list index out of range error because you try to access the list at index 2 for out_path (in your Python script).

But in your shell script you're just passing one argument in the Python script (at this line: python script.py text_out/"${name}.json").

Your first example (python script.py input_files/input.txt text_out/output.json) works ofc, because you're just passing two arguments into the Python script. That's why you can access sys.argv easily at index 1 and 2.

You should check the length of len(sys.argv) to know how many arguments are passed into the Python script.

Example Shell Script

Your shell script should look something like this to get rid if the IndexError:

for i in input_files/*.txt;
  do name=`echo "$i" | cut -d'.' -f1`
  echo "$name"1

  # Pass two args into your Python script
  python script.py input.txt output.txt
done

1 Comment

of course it is working with (python script.py input_files/input.txt text_out/output.json)

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.