Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python使用字符串检查文件内容_Python - Fatal编程技术网

Python使用字符串检查文件内容

Python使用字符串检查文件内容,python,Python,你好!!我有以下脚本: import os import stat curDir = os.getcwd() autorun_signature = [ "[Autorun]", "Open=regsvr.exe", "Shellexecute=regsvr.exe", "Shell\Open\command=regsvr.exe",

你好!!我有以下脚本:

import os
import stat

curDir = os.getcwd()

autorun_signature = [ "[Autorun]",
                      "Open=regsvr.exe",
                      "Shellexecute=regsvr.exe",
                      "Shell\Open\command=regsvr.exe",
                      "Shell=Open" ]

content = []

def read_signature(file_path):
    try:
        with open(file_path) as data:
            for i in range(0,5):
                content.append(data.readline())
    except IOError as err:
        print("File Error: "+   str(err))

read_signature(os.getcwd()+'/'+'autorun.inf')

if(content==autorun_signature):
    print("Equal content")
else:
    print("Not equal")
它打印不相等,然后我尝试了这个方法:

import os
import stat

curDir = os.getcwd()

autorun_signature =  "[Autorun]\nOpen=regsvr.exe\nShellexecute=regsvr.exe\nShell\Open\command=regsvr.exe\nShell=Open"

content = ""

def read_signature(file_path):
    try:
        with open(file_path) as data:
            content = data.read()
    except IOError as err:
        print("File Error: "+   str(err))

read_signature(os.getcwd()+'/'+'autorun.inf')

if(content==autorun_signature):
    print("Equal content")
else:
    print("Not equal")
它也打印不平等! 我想在脚本中存储autorun.inf文件的内容,每次我找到这样的文件时,我都想检查它的内容,如果它是或不是,我做不到,有人能帮我吗? autorun.inf的内容:

[Autorun]
Open=regsvr.exe
Shellexecute=regsvr.exe
Shell\Open\command=regsvr.exe
Shell=Open

这可能是因为Windows新行是
\r\n
而不是
\n

另外,您应该转义
“\”
,因此在Windows下使用
“\\”

换行符
\r\n
与Linux的
\n
不同


因此,尝试将
\n
替换为
\r\n

如果您在阅读后打印
内容,它会显示什么?您应该将文本解析为字典,然后比较。。。