0

所以我有这段代码,如果我指定一个要比较的特定文件,它基本上可以正常工作:但是一旦我创建一个变量以允许用户输入文件名,然后比较我得到以下错误。

这是我目前的代码。

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代码可以完美执行,但是当我尝试添加自己的变量时,它会崩溃。

4

1 回答 1

0

以下代码:

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)
于 2020-02-05T20:51:19.043 回答