18

How to run a python with arguments that would contain spaces? I am using MacOS

For example,

>python testProgram.py argument 1 argument 2

where "argument 1" is a single argument?

0

4 Answers 4

31

where "argument 1" is a single argument.

You've basically answered your own question there, "argument 1" is indeed a single argument.

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

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

This isn't actually a Python issue however, it depends on the shell that you're using to run the Python script.

For example, with bash, there are differences between the single and double quotes, the most important of which is probably the various expansions like $HOME - the single quoted variant does not do those expansions.

Sign up to request clarification or add additional context in comments.

3 Comments

What if I have sth like: RAW_PATH="/Volumes/TOSHIBA EXT/../" and I want to use $RAW_PATH as argument? This does not work for me.
@thigi: you would use "${RAW_PATH}" - variables work just fine within double quotes.
Okay thank you! Sorry for not trying that, but I thought that would not work and hence I did not try it! Sorry for this inconvenience...
3

Enclose your parameters that contains spaces with double quotes

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

this will work under Windows and Linux so chances are it'll be ok under Mac OS too.

Comments

2

Or using subprocess from within python itself:

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

But the other answers are more likely to be what you want.

Comments

0

Try:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.