具有等号和空格的 Python 子进程参数

2023-09-03Python开发问题
2

本文介绍了具有等号和空格的 Python 子进程参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我试图在不使用 shell=True 关键字参数的情况下运行带有 subprocess.check_output 的命令.我的命令是这样的:

I am trying to run a command with subprocess.check_output without using shell=True keyword argument. My command is this:

command --parameter="something with spaces"

有了这个:

subprocess.check_output(['command', '--parameter="something with spaces"'])

命令变成这样:

command "--parameter="something with spaces""

还有这个:

subprocess.check_output(['command', '--parameter=', 'something with spaces'])

命令变成这个(=后的空格):

command becomes to this(white space after =):

command --parameter= "something with spaces"

有没有不使用 shell=True

推荐答案

以下是你需要知道的:

空格用于分隔 shell 命令行上的参数.但是,如果您不使用 shell,则不需要转义空格.空格可以至少以两种方式转义(据我所知):使用引号(单引号或双引号)和反斜杠.

Spaces are used for separating arguments on the shell command line. However, if you are not using shell, you don't need to escape spaces. Spaces can be escaped at least two ways ( that I know of ): With quotes ( either single or double ) and the backslash .

当您将数组传递给 subprocess.check_output() 时,您已经将命令划分为子进程的参数.因此,您不需要带空格的东西"周围的引号.也就是说,您不需要转义空格.相反,正如您在结果片段中显示的那样,引号被完全视为引号:

When you pass an array to subprocess.check_output() you are already dividing the command into parameters for the subprocess. Thus, you don't need the quotes around "something with spaces". That is, you don't need to escape the spaces. Rather, the quotes are being taken quite literally as quotes as you have shown with your result snippet:

command "--parameter="something with spaces""

现在我希望你已经猜到正确答案是什么了.前方剧透:

By now I hope you have guessed what the right answer is. Spoiler ahead:

subprocess.check_output(['command', '--parameter=something with spaces'])

这篇关于具有等号和空格的 Python 子进程参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

在xarray中按单个维度的多个坐标分组
groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)...
2024-08-22 Python开发问题
15

Pandas中的GROUP BY AND SUM不丢失列
Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)...
2024-08-22 Python开发问题
17

pandas 有从特定日期开始的按月分组的方式吗?
Is there a way of group by month in Pandas starting at specific day number?( pandas 有从特定日期开始的按月分组的方式吗?)...
2024-08-22 Python开发问题
10

GROUP BY+新列+基于条件的前一行抓取值
Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)...
2024-08-22 Python开发问题
18

PANDA中的Groupby算法和插值算法
Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)...
2024-08-22 Python开发问题
11

PANAS-基于列对行进行分组,并将NaN替换为非空值
Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)...
2024-08-22 Python开发问题
10