差异检查器,输入文件名而不是文件名

问题描述 投票:0回答:1

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

这是我的当前代码。

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'))

您注意到,如果我将filename设置为standard.txt和new.txt,则代码可以完美执行,但是第二次尝试添加自己的变量,它崩溃。

python difference difference-lists
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)
© www.soinside.com 2019 - 2024. All rights reserved.