所以我有这段代码,如果我指定一个要比较的特定文件,它基本上可以正常工作:但是一旦我创建一个变量以允许用户输入文件名,然后比较我得到以下错误。
这是我目前的代码。
def open_file_and_return_list(file_path):
list = []
with open(file_path, 'r') as f:
line = f.readline()
while line:
list.append(line)
line = f.readline()
return list
def clean_new_line(list):
for i in range(len(list)):
if "\n" in list[i]:
list[i] = list[i].replace("\n", "")
return list
if __name__ == "__main__":
s1 = input("INFO: Select the first file to compare: ")
s2 = input("INFO: Select the first file to compare: ")
list1 = open_file_and_return_list(r"new.txt")
list2 = open_file_and_return_list(r"standard.txt")
maxl = max(len(list1), len(list2))
list1 += [''] * (maxl - len(list1))
list2 += [''] * (maxl - len(list2))
diff = []
diff_file = input("\nINFO: Select what to name the difference(s) : ")
open(diff_file, 'w').close()
for iline, (l1, l2) in enumerate(zip(list1, list2)):
if l1 != l2:
print(iline, l1, l2)
print(iline, l1, l2, file=open(diff_file, 'a'))
我得到的错误是:
list1 = open_file_and_return_list('r', s1)
TypeError: open_file_and_return_list() takes 1 positional argument but 2 were given
我基本上希望允许用户再次说明要比较的文件,因为它们总是有不同的名称并且是“通配符”
s1 = input("INFO: Select the first file to compare: ")
s2 = input("INFO: Select the first file to compare: ")
我究竟做错了什么?我的逻辑完全不正常吗?还是我呆滞的眼睛漏掉了一些小东西。
编辑
我正在运行的确切代码是:
elif device_type == "7":
print("\n")
print("************************************")
print("***** *****")
print("***** Comparision Checker *****")
print("***** Of Two Configs *****")
print("************************************")
print("\n")
print('\nWARNING: Discrepancies found:')
def open_file_and_return_list(file_path):
list = []
with open(file_path, 'r') as f:
line = f.readline()
while line:
list.append(line)
line = f.readline()
return list
def clean_new_line(list):
for i in range(len(list)):
if "\n" in list[i]:
list[i] = list[i].replace("\n", "")
return list
if __name__ == "__main__":
s1 = input("INFO: Select the first file to compare: ")
s2 = input("INFO: Select the first file to compare: ")
list1 = open_file_and_return_list(r"new.txt")
list2 = open_file_and_return_list(r"standard.txt")
maxl = max(len(list1), len(list2))
list1 += [''] * (maxl - len(list1))
list2 += [''] * (maxl - len(list2))
diff = []
diff_file = input("\nINFO: Select what to name the difference(s) : ")
open(diff_file, 'w').close()
for iline, (l1, l2) in enumerate(zip(list1, list2)):
if l1 != l2:
print(iline, l1, l2)
print(iline, l1, l2, file=open(diff_file, 'a'))
正如您所注意到的,如果我将文件名设置为standard.txt
并且new.txt
代码可以完美执行,但是当我尝试添加自己的变量时,它会崩溃。