Specifying arguments with spaces to run a python script

How to run python with arguments that contain spaces? I am using macos

For instance,

>python testProgram.py argument 1 argument 2

where "argument 1" is the only argument?

+5
source share
4 answers

where argument 1 is the only argument.

Basically you answered your own question, "argument 1"really is the only argument.

In other words, you need to quote it, something like:

python testProgram.py "argument 1" 'argument 2'

This is not a Python issue, but it depends on the shell you use to run the Python script.

, bash , , , , $HOME - .

+8

,

> python testProgram.py "argument 1" "argument 2"

Windows Linux, , Mac OS .

+2

Try:

>python testProgram.py "argument 1" "argument 2"
+1
source

Or using subprocessfrom python itself:

subprocess.call(['python','testProgram.py','argument 1','argument 2'])

But other answers will most likely be what you want.

+1
source

All Articles