python - 差异检查器,输入文件名而不是说明文件
问题描述
所以我有这段代码,如果我指定一个要比较的特定文件,它基本上可以正常工作:但是一旦我创建一个变量以允许用户输入文件名,然后比较我得到以下错误。
这是我目前的代码。
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
代码可以完美执行,但是当我尝试添加自己的变量时,它会崩溃。
解决方案
以下代码:
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)
推荐阅读
- python - Python3:记忆游戏中的碰撞点错误
- reactjs - DatePicker ant design - 禁用当前/今天日期
- c - 为什么将指针传递给函数时设置为(nil)?
- amazon-ec2 - 为我的 VPC 中的所有实例打开端口 22
- iis - 我是否需要为 IIS 上托管的每个 Web api 创建一个 appid?什么情况下需要使用单独的appid?
- sql - SQL Server GROUP BY Hour over Midnight
- github - 如何使用 Visual Studio 2019 中的 Team Explorer 将我的项目连接到新的存储库,而不是 Github 上的现有存储库?
- angular - NGXS 使用存储运算符更新包含对象数组的对象数组
- javascript - 如何选择 CDN 来加载 Javascript 和 CSS 库?
- vue.js - vue 没有显示正确的信息