以下是一个示例代码,用于比较两个文件,并从第三个文件中获取输入,并将最大计数写入第四个文件。
def compare_files(file1, file2, input_file, output_file):
# 读取第一个文件内容
with open(file1, 'r') as f1:
content1 = f1.read()
# 读取第二个文件内容
with open(file2, 'r') as f2:
content2 = f2.read()
# 读取第三个文件内容,获取输入
with open(input_file, 'r') as input_f:
inputs = input_f.readlines()
# 计数最大值
max_count = max(content1.count(inputs[0]), content2.count(inputs[0]))
# 将最大计数写入第四个文件
with open(output_file, 'w') as output_f:
output_f.write(str(max_count))
要使用上述代码,需要将文件路径传递给函数compare_files,并指定输出文件的路径。例如:
compare_files('file1.txt', 'file2.txt', 'input.txt', 'output.txt')
上述代码假设文件1和文件2包含要比较的文本内容,并且第三个文件input.txt包含要获取的输入。最大计数将写入第四个文件output.txt。请根据实际情况修改文件名和路径。