python - python:将文本与文件进行比较
问题描述
我想使用 python 将 shell 命令输出与文本文件进行比较,这是我的代码:
os.system('find / -perm -u=s -type f 2>/dev/null')
suid_file = open("suid_list.txt", "r")
os.system('find / -perm -u=s -type f 2>/dev/null > /tmp/test1.txt')
lines = suid_file.read().split(',')
lines2 = open('/tmp/test1.txt', 'r').read()
for y in lines:
if y in lines2:
#matched
test1.txt 的内容是:
/usr/bin/su
/usr/bin/bwrap
/usr/bin/chsh
/usr/bin/ntfs-3g
/usr/bin/kismet_cap_ti_cc_2540
/usr/bin/mount
/usr/bin/vmware-user-suid-wrapper
/usr/bin/passwd
/usr/bin/fusermount3
/usr/bin/kismet_cap_linux_bluetooth
/usr/bin/kismet_cap_nxp_kw41z
/usr/bin/kismet_cap_nrf_mousejack
/usr/bin/newgrp
/usr/bin/gpasswd
解决方案
lines2 = file_read.read()
file_read.read() 的第二次调用将返回空。您应该在循环外部调用并将结果存储在一个可以在循环内部使用的变量中。
推荐阅读
- weaviate - 如何对 Weaviate 中的另一个对象进行交叉引用?
- javascript - 如何找到可以将数组中的数字相加的所有方法
- couchbase - Couchbase operator 1.1.0 到 1.2.0 升级问题
- gdal - GDAL 3.0.1 的源安装失败,未定义对 EVP_MD_CTX_new 和 EVP_MD_CTX_free 的引用
- python - 仅适用于 Python 文件的 Python 调用图
- security - KafkaPublishRecord_2_0 JAAS 配置 sasl.jaas.config
- javascript - JS 和 .NET 夏令时行为的区别
- python - ValueError:新数组的总大小必须保持不变(https://github.com/ajgupta93/enhanced-view-synthesis/blob/master/code/viewsyn_model.py)
- javascript - x-Values 数据未在 Highcharts、Javascript 中显示
- android - 为android创建发布版本时反应本机网络错误