【问题标题】:python : compare a text with filepython:将文本与文件进行比较
【发布时间】:2021-01-16 16:02:45
【问题描述】:

我想使用 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

【问题讨论】:

    标签: python python-3.x file compare


    【解决方案1】:
       lines2 = file_read.read()
    

    第二次调用 file_read.read() 将返回空。 您应该在循环外部调用并将结果存储在一个可以在循环内部使用的变量中。

    【讨论】:

    • 我的 suid_file.txt 是这样的:root, ALL, gpasswd, /usr/bin/gpasswd
    • 1. /tmp/test1.txt 的内容是什么? 2. 你能用正确的代码编辑你的问题吗? (循环外的file_read.read())
    • 查看上面的信息
    • 我确切地说文件'/tmp/test1.txt'中的所有行都用换行符分隔(我认为粘贴时出现堆栈错误):这是Linux shell suid二进制文件返回。
    猜你喜欢
    • 1970-01-01
    • 2016-04-07
    • 2018-08-21
    • 2021-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-14
    相关资源
    最近更新 更多