差异检查器,输入文件名而不是声明文件
Difference checker, take input of file name instead of stating file
所以我有这段代码,如果我指定要比较的特定文件,它基本上可以正常工作:
但是一旦我创建了一个变量以允许用户输入文件名,然后比较我得到以下错误。
这是我目前的代码。
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
我基本上想让用户再次声明要比较的文件,因为它们总是有不同的名称并且是 "wild cards"
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
,代码将完美执行,但第二次我尝试添加自己的变量时,它崩溃了。
以下代码:
list1 = open_file_and_return_list('r', s1)
表示函数调用open_file_and_return_list()
,第一个参数是'r'
,第二个参数是s1
。但是,该函数在代码顶部定义如下:
def open_file_and_return_list(file_path):
告诉Python函数应该只允许一个参数存储为变量file_path
。结果,Python 将 'r'
存储为 file_path
,并且不知道如何处理原始函数调用中的 s1
。
根据其余代码的编写方式,'r'
似乎是像 r"new.txt"
这样的语句的一部分。但是,对于问题中提供的特定代码,不需要 'r'
并且应该可以只传递存储在 s1
:
中的文件路径
list1 = open_file_and_return_list(s1)
所以我有这段代码,如果我指定要比较的特定文件,它基本上可以正常工作: 但是一旦我创建了一个变量以允许用户输入文件名,然后比较我得到以下错误。
这是我目前的代码。
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
我基本上想让用户再次声明要比较的文件,因为它们总是有不同的名称并且是 "wild cards"
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
,代码将完美执行,但第二次我尝试添加自己的变量时,它崩溃了。
以下代码:
list1 = open_file_and_return_list('r', s1)
表示函数调用open_file_and_return_list()
,第一个参数是'r'
,第二个参数是s1
。但是,该函数在代码顶部定义如下:
def open_file_and_return_list(file_path):
告诉Python函数应该只允许一个参数存储为变量file_path
。结果,Python 将 'r'
存储为 file_path
,并且不知道如何处理原始函数调用中的 s1
。
根据其余代码的编写方式,'r'
似乎是像 r"new.txt"
这样的语句的一部分。但是,对于问题中提供的特定代码,不需要 'r'
并且应该可以只传递存储在 s1
:
list1 = open_file_and_return_list(s1)