Passing Arguments to Python Script

Hi,

I have an app that executes a python script. I call the python script from

shell.execute(thePythonFilePath)

This works if I add the word "python " to the front of the path. Now I would like to pass two parameters, also file paths, to the python script.

I have tried this


shell.execute(thePythonFilePath,1filepath,2filepath) // all as strings

shell.execute(thePythonFilePath+" "+1filepath+" "+2filepath) // all as strings

shell.execute(thePythonFilePath+"'"+1filepath+"'"+2filepath) // all as strings

All these fail. I have looked at lots of documentation but seam to get conflicting answers. Can some shed some light?

Nigel

I would not expect #1 or #3 to work…

#2 “should” work, assuming “thepythonfilepath” starts with "python " followed by the script name/path

#3 might work under the same conditons, and if you changed "’ " to “’ '” instead (you have a single quote starting each argument, and no ending quote)

type replacing “shell.execute” with “msgbox” so you can “SEE” the full command line
and then paste it into “terminal” to see that it truly does work

#2 is the MOST likely but

  1. make sure the command is started with the actual full path to PYTHON itself since a SHELL does NOT have any of your environment set up (ie/ the paths to executables etc) You can get this by opening Terminal on macOS and typing in “which python” and you will get the full path to it
  2. the path to the python file needs to be a full & properly escaped POSIX path (this is FolderItem.ShellPath)
  3. the paths to the other two items also need to be fully escaped POSIX paths (this is FolderItem.ShellPath)

OK so whats an escaped posix path ?
Well IF you are on macOS and open Terminal and drag file in there that has special characters in the name (spaces etc) you can see the name may have \ in it (Windows & linux consoles work similarly)
These act as “escapes” saying “the next character LITERALLY” so you can embed a number of special characters in a name and they get interpreted right and NOT handled by the command interpreter (bash, csh, zsh etc)

so you might end up with

    shell.execute(thePosixPathToPython + " " + thePosixPathToThePythonFilePath+" "+PosixFilepath1+" "+PosixFilepath2) // all as strings

which might look like

   shell.execute("/usr/bin/python" + " " + "/Users/nigle/python\\ scripts/coolstuff+" "+"/Users/nigle/Documents/input1" + "/User/nigle/Documents/data\\ files/file2") 

now … you CAN sometimes get away with quoting file paths by surrounding them with double quotes( ")

    shell.execute(thePosixPathToPython + " """ + ThePythonFilePath+""" """+Filepath1+""" """+Filepath2 +"""") // all as strings

which might look like

   shell.execute("/usr/bin/python" + " " + ""/Users/nigle/python scripts/coolstuff+"" "+""/Users/nigle/Documents/input1"" + ""/User/nigle/Documents/data files/file2"") 

BEWARE that copying & pasting from the forums may turn all double quotes into “smart quotes”

Hi,

That’s great.

I am already sorting the Posix path as I add "python " to the front of the shell path to the script. My python script writer told me to put that there and now I know why.

I will now investigate further.

Nigel

So…

I have inserted "\ " between shell paths and that appears to work so far, I need my python guy to see if the argument go through, I’m not getting an error.

"\ " would make sense as a space on it’s own was being stripped out, but adding \ forces it to be read?

Thanks for the detailed prompt replies.

No, you would use "\ " to preserve a space within a path. Normally, a space separates parts of the command line. Ie.,

/some/path/with a space/here

would be interpreted as a command “/some/path/with” and 2 arguments, “a” and “space/here”

to keep it as a single path, you have to escape the spaces

/some/path/with\ a\ space/here

But the spaces between paths do not need to be (should not be) escaped. They are meant to separate the pieces of the command line.

Hi Tim,

Ahhh, that makes sense, now I see why all my other versions should have been more successful maybe the fault is in the python bit.

"\ " = ignore space
" " = new piece of information

Ok, so time for some more testing.

Nigel

1 Like

Hi,

So I got a python script that returned the two strings i sent it. I used the spaces as explained and of course it all worked.

Thank you for all detailed explanations.

Cheers.

Nigel