繁体   English   中英

我如何在 Python 中执行 comm Linux 命令

[英]How can i execute comm Linux command in Python

我想从 File1 中提取 File2 中不存在的行

文件 1

a  
b  
c  

文件 2

a  
c  

所以输出应该是:

b  

bash 中一种可能的命令是:

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

它在 bash 中运行良好,但我不知道如何正确地在 Python 中实现。

我试过

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

并且不工作。 任何提示?

纯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))

或者使用更少的内存开销:

with open('file1') as f, open('file2') as f2:
    lines1 = set(map(str.rstrip, f))
    print(lines1.difference(map(str.rstrip, f2)))

如果您必须使用 shell,请安全地使用:

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

也就是说:不要将文件名作为代码的一部分传入,而是将它们作为带外变量传递,这样 shell 就无法解释它们的名称。

它不起作用,因为您需要使用bash运行命令:

os.system("bash -c 'comm -23 <(sort File1) <(sort File2) > File'")

通常, os.system()使用sh运行命令。 但是, bashsh之间几乎没有什么不同。

所以在这种情况下,我使用bash -c 'command' call bash run 命令。 那么它就可以工作了。

bash手册:

-c如果存在-c选项,则从第一个非选项参数command_string读取命令。

如果command_string之后有参数,则将它们分配给位置参数,从$0开始。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM