pythondifferencedifference-lists

Difference checker, take input of file name instead of stating file


So I have this code, It basically works fine if I specify a specific file to compare to: but once I create a variable to allow a user to input the file name, and than compare I get the following errors.

Here is my code currently.

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

The error I am getting is:

 list1 = open_file_and_return_list('r', s1)
 TypeError: open_file_and_return_list() takes 1 positional argument but 2 were given

I basically want to allow the user to state again the files to compare as they will always have different names and be "wild cards"

    s1 = input("INFO: Select the first file to compare: ")
    s2 = input("INFO: Select the first file to compare: ")

What am I doing wrong? Is my logic completely out of wack? Or are my glazed over eyes missing something small.

EDIT

The exact code I am running is:

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

As you notice, if I set filename to standard.txt and new.txt the code executes flawlessly, but the second I am attempting to add my own variables, it crashes.


Solution

  • The following code:

    list1 = open_file_and_return_list('r', s1)
    

    Implies that the function is called open_file_and_return_list(), the first argument is 'r', and the second argument is s1. However, the function is defined at the top of the code as so:

    def open_file_and_return_list(file_path):
    

    which tells Python that the function should only allow one argument to be stored as the variable file_path. As a result, Python stores the 'r' as the file_path and doesn't know what to do with the s1 in the original function call.

    Based on the way the rest of the code is written, it appears that the 'r' was meant to be part of a statement like r"new.txt". However, for the specific code provided in the question, the 'r' is not needed and it should be possible to just pass in the file path stored in s1:

    list1 = open_file_and_return_list(s1)