newMember
newMember

Reputation: 41

Impossible to drag file on python script

I used to handle some files with .py scripts using "sys" library

import sys

if len(sys.argv) > 0:
    name = str(sys.argv[1])

Now for some reason this way doesn't work. When i drag files .py script stays inactive for interaction. How can i fix this and what are possible reasons for such behaviour? I use python 3.6.2

Upvotes: 2

Views: 391

Answers (1)

Enthus3d
Enthus3d

Reputation: 2165

If you mean that when you drag the file to execute, it hangs, this is probably because your file needs arguments to process. If you attempt to run the file in a debugger with no extra arguments you will probably come across the following error:

list index out of range

File "C:\SO\running_stuff.py", line 4, in

name = str(sys.argv[1])

This is because your

name = str(sys.argv[1])

line is trying to grab the 1st index, which does not exist with no arguments (prompting the list index out of range error). Try

str(sys.argv[0]

instead, since you might be looking for the 0th index, not the first. If you are actually looking for the first index, not the 0th, you can require your line

if len(sys.argv) > 0:

to be

if len(sys.argv) > 1:

instead.

Upvotes: 1

Related Questions