[英]How can i execute comm Linux command in Python
I want to extract lines from File1 which are not present in File2我想从 File1 中提取 File2 中不存在的行
File1文件 1
a
b
c
File2文件 2
a
c
so the output should be:所以输出应该是:
b
One possible command in bash is: bash 中一种可能的命令是:
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.它在 bash 中运行良好,但我不知道如何正确地在 Python 中实现。
I've tried with我试过
import os
os.system("comm -23 <(sort File1) <(sort File2) > File")
And is not working.并且不工作。 Any hint?任何提示?
How about a pure python solution?纯python解决方案怎么样?
with open('file1', 'r') as f:
lines1 = set(f.read().splitlines())
with open('file2', 'r') as f:
lines2 = set(f.read().splitlines())
print(lines1.difference(lines2))
Or with less memory overhead:或者使用更少的内存开销:
with open('file1') as f, open('file2') as f2:
lines1 = set(map(str.rstrip, f))
print(lines1.difference(map(str.rstrip, f2)))
If you must use a shell, do it safely:如果您必须使用 shell,请安全地使用:
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.也就是说:不要将文件名作为代码的一部分传入,而是将它们作为带外变量传递,这样 shell 就无法解释它们的名称。
It doesn't work because you need use bash
run the command:它不起作用,因为您需要使用bash
运行命令:
os.system("bash -c 'comm -23 <(sort File1) <(sort File2) > File'")
Normally, os.system()
use sh
run the command.通常, os.system()
使用sh
运行命令。 But however, there's little different between bash
and sh
.但是, bash
和sh
之间几乎没有什么不同。
So in this case, I used bash -c 'command'
call bash
run the command.所以在这种情况下,我使用bash -c 'command'
call bash
run 命令。 Then it could works.那么它就可以工作了。
From the manual of bash
:从bash
手册:
-c
If the-c
option is present, then commands are read from the first non-option argumentcommand_string
.-c
如果存在-c
选项,则从第一个非选项参数command_string
读取命令。If there are arguments after the
command_string
, they are assigned to the positional parameters, starting with$0
.如果command_string
之后有参数,则将它们分配给位置参数,从$0
开始。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.