Skip to content
Advertisement

How can i execute comm Linux command in Python

I want to extract lines from File1 which are not present in File2

File1

a  
b  
c  

File2

a  
c  

so the output should be:

b  

One possible command in bash is:

comm -23 <(sort File1) <(sort File2) > File  

And it works perfectly well in bash, but I don’t know how correctly to implement in Python.

I’ve tried with

import os  
os.system("comm -23 <(sort File1) <(sort File2) > File")  

And is not working. Any hint?

Advertisement

Answer

If you must use a shell, do it safely:

subprocess.call(['bash', '-c',
    'comm -23 <(sort "$1") <(sort "$2") >"$3"', '_',
    infile1_name, infile2_name, outfile_name])

That is to say: Instead of passing the filenames in as part of your code, pass them as out-of-band variables such that their names can’t be interpreted by the shell.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement