0

I'm just learning to code in Python / Selenium.

I achieved the first basic thing I wanted to do, but I have a problem regarding a script within a Windows batch that works fine on one PC but not on the other, and although I read a lot I still don't understand why!

I created a script which works with a settings file.

I try to retrieve it with this piece of code :

with open(str(sys.argv[1]), "r") as settings:

and I batch the call to the script within a .cmd file as follows :

start "py" "script.py" "script1.txt"
start "py" "script.py" "script2.txt"

On a first computer, everything works, the script is correctly been launched twice, each time with the correct setting file.

On a second computer:

DevTools listening on ws://127.0.0.1:52471/devtools/browser/f0cc9389-d887-4d48-ab52-87d10f867d5a
Traceback (most recent call last):
  File "C:\Users\Jeremie\Dropbox\perso\TIX\script.py", line 19, in <module>
    with open(str(sys.argv[1]), "r") as settings:
IndexError: list index out of range

How can this be?

The computers are both on Windows 10.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
jeremoquai
  • 101
  • 2
  • 10
  • 1
    I'll try printing `sys.argv` on both computers and see the difference... – Moshe Slavin Feb 17 '19 at 11:56
  • in one case still : File "C:\Users\Jeremie\Dropbox\perso\TIX\script.py", line 19, in print(sys.argv[1]) IndexError: list index out of range in the other : ['script1.txt'] and ['script2.txt'] ! – jeremoquai Feb 17 '19 at 12:01
  • 1
    before you `open` the file see what files you have! this can indicate why you get an error... use this line above the `open` line `print(sys.argv)` this will give you a list of all the py args... – Moshe Slavin Feb 17 '19 at 12:04
  • moshe i commented everything else than the print : this is what i get when i only have the print – jeremoquai Feb 17 '19 at 12:05
  • Try running `assoc .py` to see extension association (you should see something like `.py=Python.File`, extension/association) and with the association returned run `ftype Python.File` (or your association) to see the executed command line. See if in both computers arguments are correcly passed in the executed command line. – MC ND Feb 17 '19 at 12:14
  • Alternatively, change the direct call to the `.py` file in your batch files to a full path reference to the python executable passing the script file and arguments. – MC ND Feb 17 '19 at 12:16
  • thanks for trying to help me | .py=Python.File on the PC which was failing | .py=Python.File on the other | so the same – jeremoquai Feb 17 '19 at 12:17
  • @MCND i ttried several syntaxes with call, start, and nothing, but i kept on having different erreor messages ! what do you suggest ? i'm kind of lost with the quotes i should use or not, as an example – jeremoquai Feb 17 '19 at 12:18
  • Both cases can be `Python.File`, but if you run `ftype Python.File` to see the configured command line maybe you can find on one of the computers there is not argument passing when executing `python.exe`. You are not directly calling the python executable file passing the arguments but letting the OS do it for you so you depend on what the configuration on your computers is. – MC ND Feb 17 '19 at 12:23
  • Python.File="C:\WINDOWS\py.exe" "%L" %* in PC1 (failing) | Python.File="C:\WINDOWS\py.exe" "%L" %* in PC2 (succeeding) | same – jeremoquai Feb 17 '19 at 12:37
  • There are some advices in python documentation about using `py.exe` under windows. Try with full paths `start "py" "c:\python\python.exe" "c:\somewhere\script.py" "c:\some\place\script.txt"` If even this does not work, the most probable cause is you have a problem with your install. – MC ND Feb 17 '19 at 12:55
  • thanks for this last advice : i was not willing to put the whole path because - as an example - the files can be in a dropbox forled which doesn't have the same path depending on the PC i will anyway try this and, if it fails again, reinstall the PC on which the script is failing thanks – jeremoquai Feb 17 '19 at 12:58
  • hi i added a 4th argument in the start command and it worked ! start "script4" "py" "script.py" "script4.txt" : it seems like the start NEEDS a title and that it was interpreting "py" as the title and by a way of consequence launching "script.py" "script1.txt" and it lead to failure – jeremoquai Feb 19 '19 at 12:05
  • i still don't understand why it was behaving differently, but at least now it works – jeremoquai Feb 19 '19 at 12:06

0 Answers0