为什么 f1= open(filethub) 在几次成功迭代后仍然失败?
import string
import shutil
import difflib
oldPath = input("What is the old directory?")
thubPath = input("What is the thub directory?")
toFile = input("What directory do you want the output file to go to? ")
def find(name, path):
for root, dirs, files in os.walk(path):
if name in files:
tf = os.path.join(root, name)
return tf
def main():
with open('difftext.txt', 'a')as outfile:
for root, dirs, files in os.walk(oldPath):
for file in files:
fileold = file
filethub = find(fileold, thubPath)
fileoldn = os.path.join(root,fileold)
print(filethub)
f1 = open(filethub)
f2 = open(fileoldn)
outfile.write("|||||||||||||||||||||||||||||||||||\n")
outfile.write("\nComparing files \n")
outfile.write("-----------------------------------\n")
outfile.write(" > " + str(os.path.basename(filethub))+'\n')
outfile.write(" < " +str(os.path.basename(fileoldn))+'\n')
outfile.write("-----------------------------------\n")
# Read the first line from the files
f1_line = f1.readline()
f2_line = f2.readline()
# Initialize counter for line number
line_no = 1
# Loop if either file1 or file2 has not reached EOF
while f1_line != '' or f2_line != '':
# Strip the leading whitespaces
f1_line = f1_line.rstrip()
f2_line = f2_line.rstrip()
# Compare the lines from both file
if f1_line != f2_line:
创建此脚本以遍历包含相同文件的两个版本的两个目录,并在单个文件中打印出差异。oldPath 和 thubpath 是用户输入值,例如
慕斯王
忽然笑
一只甜甜圈
相关分类