Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.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中每10秒更新一次单词的文件进行比较?_Python_Python 3.x - Fatal编程技术网

您能否将一个具有固定单词列表的文件与一个在python中每10秒更新一次单词的文件进行比较?

您能否将一个具有固定单词列表的文件与一个在python中每10秒更新一次单词的文件进行比较?,python,python-3.x,Python,Python 3.x,此方法可用于比较它们吗? 我尝试了很多方法,但没有找到任何解决办法 如果有人能帮助我,那就太好了! 我对python相当陌生上面的代码会将文件中的每一行与其他文件中的每一行进行比较。您可能希望通过相同的索引对它们进行比较 f1 = open("C:/Users/user/Documents/intro.txt", "r") f2 = open("C:/Users/user/Desktop/intro1.txt", "r&q

此方法可用于比较它们吗?
我尝试了很多方法,但没有找到任何解决办法 如果有人能帮助我,那就太好了!
我对python相当陌生

上面的代码会将文件中的每一行与其他文件中的每一行进行比较。您可能希望通过相同的索引对它们进行比较

f1 = open("C:/Users/user/Documents/intro.txt", "r")  
f2 = open("C:/Users/user/Desktop/intro1.txt", "r")  
  
i = 0
  
for line1 in f1:
    i += 1
      
    for line2 in f2:
          
        
        if line1 == line2:  
            print("Line ", i, ": IDENTICAL")       
        else:
            print("Line ", i, ":")
            print("\tFile 1:", line1, end='')
            print("\tFile 2:", line2, end='')
        break
f1.close()                                       
f2.close() 

除了上面的注释外,下面是易于对文件进行哈希的代码:

with open("C:/Users/user/Documents/intro.txt", "r") as f1:
    lines1 = f1.readlines()

with open("C:/Users/user/Desktop/intro1.txt", "r") as f2:
    lines2 = f2.readlines()

length1 = len(lines1)
length2 = len(lines2)

if length1 == length2:
    for i in range(length1):
        line1 = lines1[i]
        line2 = lines2[i]

        if lines1[i] == lines2[i]:
            print("Line ", i, ": IDENTICAL")
        else:
            print("Line ", i, ":")
            print("\tFile 1:", line1, end='')
            print("\tFile 2:", line2, end='')
            break
else:
    print("Unequal length: ", length1, " ", length2)

查看关于在写入文件时写入文件的线程。至于静态文件,您只需将数据存储在某种数据结构(如
列表
)中,并将其用于比较:)是否需要逐行比较文件并找出所有不同的行。。还是比较两个文件?我想在最后一种情况下,您可以简单地将文件的哈希值与进行比较。@Bohdan Yes确切地说,文件行需要逐行比较,因此,如果文件中有不同的行在不断更新,则应将其导出到不同的文件中,说明这些行是正确的different@gmdev是的,我正在调查你建议的线。非常感谢。
import hashlib

def hash_file(filename):
   """"This function returns the SHA-1 hash
   of the file passed into it"""

   # make a hash object
   h = hashlib.sha1()

   # open file for reading in binary mode
   with open(filename,'rb') as file:

       # loop till the end of the file
       chunk = 0
       while chunk != b'':
           # read only 1024 bytes at a time
           chunk = file.read(1024)
           h.update(chunk)

   # return the hex representation of digest
   return h.hexdigest()