Python 3,2字符串作为子进程的文件参数

时间:2018-03-10 23:41:55

标签: python-3.x subprocess stdin popen

子进程调用Diffutils,它接受​​两个文件。

  

Diff.exe 1.txt 2.txt

并返回它们之间的差异。

以下代码有效......

process = subprocess.Popen(relativePath1 + 'diff.exe ' + relativePath + '/1.txt ' + relativePath + '/2.txt', cwd=relativePath1, stdout=subprocess.PIPE)
process.wait()
print(process.stdout.read().decode())

我想用字符串而不是文件目的地替换relativePath + '/1.txt ' + relativePath + '/2.txt'

我发现这个类似的帖子Python string as file argument to subprocess接受了一个参数...但是无法通过传递两个字符串来替换diff.exe请求的两个路径。

One = 'bla bla bal bla'.encode()
Two = 'bla xxx bal bla'.encode()

p = subprocess.Popen([relativePath1 + 'diff.exe'], shell=False, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, stdin=[subprocess.PIPE, subprocess.PIPE])
p.communicate(input=One, Two)

0 个答案:

没有答案