如何在python3中将所有参数作为一个?

问题描述 投票:0回答:1

我的 python 3 脚本接收许多参数,数量事先未知。 我知道如何使用 argparse 或 argv[x] 单独读取它们,但如何将它们全部作为一个字符串获取?

例如与

python3 p.py first -second and more -and maybe another okay thisisthelast one

我想得到一个字符串:

first -second and more -and maybe another okay thisisthelast one
python-3.x arguments
1个回答
0
投票

可以使用python3的sys模块。

import sys
print(*sys.argv[1:])

示例输出:

python3 test.py hello -v
hello -v
© www.soinside.com 2019 - 2024. All rights reserved.