Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specifying arguments with spaces for running a python script

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?

like image 865
ssk Avatar asked Aug 10 '12 02:08

ssk


People also ask

How do you pass a space in a command line argument?

Note : You pass all the command line arguments separated by a space, but if argument itself has a space then you can pass such arguments by putting them inside double quotes “” or single quotes ”.


2 Answers

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.

like image 63
paxdiablo Avatar answered Oct 24 '22 11:10

paxdiablo


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.

like image 45
Levon Avatar answered Oct 24 '22 12:10

Levon