gpt4 book ai didi

python - 当 arg 已包含 ""和 ' ' 引号时,如何将字符串 arg 提交到 subprocess.run(arg, shell=True) ?

转载 作者:行者123 更新时间:2023-12-01 00:35:58 25 4
gpt4 key购买 nike

我需要使用 python 3.6 subprocess.run() 函数发出以下命令:

gsettings set org.gnome.shell enabled-extensions "['apple@gmail.com', 'orange@yahoo.com']"

由于在该命令的列表元素中找到了@符号,因此run()函数的shell kwarg必须设置为True,即subprocess。运行(arg,shell=True)。因此,该函数的语法规则要求上述命令以字符串形式提交,即

arg = "gsettings set org.gnome.shell enabled-extensions "['apple@gmail.com', 'orange@yahoo.com']" "

这就是我的困境。整个命令的字符串引号“”与原始命令中的字符串引号“”冲突。我该如何克服这个问题?

最佳答案

使用 \ 转义 "':\"\' .

示例:

>>> v = "'\"\'"
>>> [ord(i) for i in v]
[39, 34, 39]

所以在你的情况下:

arg = "gsettings set org.gnome.shell enabled-extensions \"['apple@gmail.com', 'orange@yahoo.com']\" "

关于python - 当 arg 已包含 ""和 ' ' 引号时,如何将字符串 arg 提交到 subprocess.run(arg, shell=True) ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57759123/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com