使用 subprocess.Popen() 从 bash shell 转换为 python3

convert from bash shell to python3 using subprocess.Popen()

我正在努力从 bash shell 转换为 python3。

这是我要转换为 python 的 shell 命令:

cat $outDir/aDir/* | cut -f2 | sort -u > $outDir/outFile.txt

我已经在使用 subprocess.call() 并且它有效,但我想知道如何使用 Popen() 实现它。

这是我的代码,但它不起作用:

import subprocess
import glob

filePath = outDir + 'aDir/*'
outFilePath = outDir + '/outFile.txt'

fileList = []
for files in glob.glob(filePath):
    fileList.append(files)
with open(files, 'r') as inFile, open(outFilePath, 'w') as outFile : 
  p = subprocess.Popen(['cat'], stdin=inFile, stdout=subprocess.PIPE)   
  p2 = subprocess.Popen(['cut', '-f2'], stdin = p1.stdout, stdout=subprocess.PIPE)
  p3 = subprocess.Popen(['sort', '-u'], stdin = p2.stdout, stdout = outFile)

你能解释一下为什么 shell=True 有害吗?我在很多答案中看到了,但不知道为什么...

谢谢。

您需要将文件列表传递给 cat 所以

subprocess.Popen(['cat'], stdin=inFile, stdout=subprocess.PIPE)

应该变成

subprocess.Popen(['cat'] + [fileList], stdout=subprocess.PIPE)

因此不再需要 inFile

所以,总而言之

import subprocess
import glob

filePath = outDir + '/aDir/*'
outFilePath = outDir + '/outFile.txt'

fileList = glob.glob(filePath)
with open(outFilePath, 'w') as outFile: 
  subprocess.Popen(['cat'] + [fileList], stdout=subprocess.PIPE)
  p2 = subprocess.Popen(['cut', '-f2'], stdin = p1.stdout, stdout=subprocess.PIPE)
  p3 = subprocess.Popen(['sort', '-u'], stdin = p2.stdout, stdout = outFile)

只使用 shell=True 并保留管道怎么样?

with open(files, 'r') as inFile, open(outFilePath, 'w') as outFile : 
  p = subprocess.Popen('cut -f2 | sort -u', shell=True, stdin=filePath, stdout=subprocess.PIPE)
  p.communicate()

或者更简单地说:

p = subprocess.Popen("cat {} | cut -f2 | sort -u > '{}'".format(filePath, outFilePath), shell=True)
p.communicate()

或者,甚至 更多(感谢 @tripleee!):

subprocess.call("cat {} | cut -f2 | sort -u > '{}'".format(filePath, outFilePath), shell=True)

至于 shell=True,唯一的危险是您的输入不安全。我建议用单引号引用所有输入,并转义和清理所有输入。