使用空格指定参数以运行 python 脚本 [英] Specifying arguments with spaces for running a python script
问题描述
如何使用包含空格的参数运行 python?我使用的是 MacOS
How to run a python with arguments that would contain spaces? I am using MacOS
例如
>python testProgram.py argument 1 argument 2
其中参数 1"是单个参数?
where "argument 1" is a single argument?
推荐答案
其中参数 1"是单个参数.
where "argument 1" is a single argument.
您基本上已经在那里回答了自己的问题,"argument 1"
确实是一个参数.
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'
这实际上不是 Python 问题,但这取决于您用来运行 Python 脚本的 shell.
This isn't actually a Python issue however, it depends on the shell that you're using to run the Python script.
比如bash
,单引号和双引号是有区别的,其中最重要的大概就是各种扩展比如$HOME
——单引号变体不做这些扩展.
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.
这篇关于使用空格指定参数以运行 python 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!