1

When I have the following code portion in python and suppose we execute this python file with a input file input.txt as the first command line argument.

import os
import sys
with open(sys.argv[1],"r") as input:

Suppose the name of the python file is file.py and input.txt is the argument to this, so the command is as follows,

>>file.py input.txt

I am getting the following error:

Traceback (most recent call last):
File "C:\Users\hdutta\Desktop\file.py", line 3, in <module>
with open(sys.argv[1],"r") as input:
IOError: [Errno 2] No such file or directory: ' input.txt'

Looks like it is taking an extra space at the first argument. Why is it so? Am I missing something. This was working before. But I have a new system now and I am facing this issue.

I got some clue in the link -- Python argparse adds extra space before an argument

But I am unable to modify sys.argv = if there is any way to do that.

1 Answers1

0

Hmm weird

But you could do:

sys.argv[1].lstrip().strip() # Will remove any spaces to left / right of the string. 
alexisdevarennes
  • 5,437
  • 4
  • 24
  • 38