I'm struggling with converting from bash shell to python3.
Here's shell command that I want to convert to python:
cat $outDir/aDir/* | cut -f2 | sort -u > $outDir/outFile.txt
I already use subprocess.call()
and it worked but I want to know how make it with Popen().
Here's my code which didn't work :
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)
and could you explain why shell=True
is harmful? I saw it in many answers but don't know why...
Thank you.