| 
 | 
 
 
 楼主 |
发表于 2022-1-19 13:02:54
|
显示全部楼层
 
 
 
 
大佬,我改进了一下我的代码,但现在出了个死循环,能否请你帮我看下代码? 
 
- def filename():
 
 -     global name1,name2
 
 -     name1 = input("Please enter the first file name.\n")
 
 -     name2 = input("Please enter the other file name.\n")
 
 -     
 
 -     return name1, name2
 
  
- def check():
 
 -     if name1.split(".")[1] == "txt" and name2.split(".")[1] == "txt":
 
 -         return True
 
  
-     else:
 
 -         print("Two files are not in the same format.")
 
 -         return False
 
  
- def compare():
 
 -     if check() == True:
 
  
-         file1 = open("E:\Code\Python" + "\" + name1,'r',encoding = 'utf-8')
 
 -         file2 = open("E:\Code\Python" + "\" + name2,'r',encoding = 'utf-8')
 
  
-         content1 = file1.readlines()
 
 -         aftercontent1 = []
 
 -         for line in content1:
 
  
-             while True:
 
 -                 line.strip()
 
 -                 line.replace('\n','')
 
 -                 line.replace('\t','')
 
  
-                 if '\n' in line and '\t' in line:
 
 -                     continue
 
 -                 else:
 
 -                     break
 
  
 
-             aftercontent1.append(line)
 
  
-         print(aftercontent1)
 
  
-         content2 = file2.readlines()
 
 -         aftercontent2 = []
 
 -         for line in content2:
 
  
-             while True:
 
 -                 line.strip()
 
 -                 line.replace('\n','')
 
 -                 line.replace('\t','')
 
  
-                 if '\n' in line and '\t' in line:
 
 -                     continue
 
 -                 else:
 
 -                     break
 
  
-             aftercontent2.append(line)
 
  
-         print(aftercontent2)
 
  
-         if aftercontent1 == aftercontent2:
 
 -             print("The contents are the same.")
 
 -         else:
 
 -             print("The contents are not the same.")
 
  
-         file1.close()
 
 -         file2.close()
 
  
-     else:
 
 -         return
 
  
- if __name__ == "__main__":
 
 -     filename()
 
 -     check()
 
 -     compare()
 
  
  复制代码 |   
 
 
 
 |