Python 字符串中的多个变量

Python 字符串中的多个变量,python,subprocess,Python,Subprocess,我想从Python运行一些bash命令。我已经能够用subprocess.Popen完成这项工作。在某些情况下,我希望向命令添加一些参数,如'stdin'或'cwd'。问题是我想自动化一些东西,所以我想把命令及其参数放在一个文件中 我使用的结构如下: this is a command that I want to execute, stdin=example this is another command I execute a third command, cwd=folder/ line

我想从Python运行一些bash命令。我已经能够用
subprocess.Popen
完成这项工作。在某些情况下,我希望向命令添加一些参数,如'stdin'或'cwd'。问题是我想自动化一些东西,所以我想把命令及其参数放在一个文件中

我使用的结构如下:

this is a command that I want to execute, stdin=example
this is another command
I execute a third command, cwd=folder/
line = line.split(",", 1)
cmd = subprocess.Popen(line[0].split(" "), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
在我的python脚本中,我逐行阅读,并执行如下操作:

this is a command that I want to execute, stdin=example
this is another command
I execute a third command, cwd=folder/
line = line.split(",", 1)
cmd = subprocess.Popen(line[0].split(" "), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
我想再添加一个参数,使其他参数成为泛型参数。我知道有点像

cmd = subprocess.Popen(line[0].split(" "), stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd="folder/")
cmd = subprocess.Popen(line[0].split(" "), stdout=subprocess.PIPE, stderr=subprocess.PIPE, line[1].split(",")) (maybe I should put another delimiter for the args)
工作,但我想把

cmd = subprocess.Popen(line[0].split(" "), stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd="folder/")
cmd = subprocess.Popen(line[0].split(" "), stdout=subprocess.PIPE, stderr=subprocess.PIPE, line[1].split(",")) (maybe I should put another delimiter for the args)
但这不起作用


我不知道该如何表达,也不知道它使用的具体概念是什么,因此如果能从这个意义上完整地给出答案,我将不胜感激。

当执行
line=line.split(“,”,1)
时,它将创建一个包含2个元素的列表

对于第一个条目,它是
[“这是我想要执行的命令”,“stdin=example”]

但是请注意,在第二个元素(“stdin=example”)中,第一个字符是一个空格。这是因为调用
line.split()
时只使用逗号作为分隔符,而不是逗号和空格

解决方法是将
,“
更改为
,”


不确定这是否是导致你的问题的原因……/P>诚实问题:如果你正在使用BASH代码使用<代码>子进程。Popen < /代码>一行一行,为什么不只是使用一个BASH脚本呢?我需要在中间检查一些输出的东西,比如错误之类的东西。我对bash知之甚少,并且发现我可以用python实现它。另外,当这个过程完成时,我需要验证另一个与bashwell无关的东西,您可以从Python运行整个脚本。然后做其他事情。你的意思是从bash运行它,然后再做其他事情吗?不是。我尝试删除那个空格并抛出“位置参数跟在关键字参数后面”。@DaniSeidler Right。然后像这样尝试:

cmd=subprocess.Popen(第[0]行)。split(“”),第[1]行。split(“”),stdout=subprocess.PIPE,stderr=subprocess.PIPE)
(基本上移动关键字参数前面的位置)