1

作为我应该编写的程序的一部分,我需要帮助尝试让 Python 忽略文件中的某些字符串。该作业称为:“高级抄袭检测器”,以下是详细信息:

编写一个程序,作为更高级的剽窃检测器。为了欺骗剽窃检测软件,学生们经常在他们的工作代码中插入注释。您的程序应该从键盘读取两个文件名,并在忽略注释的情况下显示两个文件是否相同。(为简单起见,您无需担心文档字符串和空行)

例如,如果一个文件具有以下代码:

n = int(input("Enter the number of cookies"))
print("I ate", n, "cookies)

另一个文件有:

#get user input
n = int(input("Enter the number of cookies"))
#print number of cookies eaten
print("I ate", n, "cookies)

程序应该打印出这些文件是相同的。

所以这就是问题所在。下面是我到目前为止的代码,我只是卡住了:

f1 = open(input(), "r")
f2 = open(input(), "r")

contentsf1 = f1.readlines()
contentsf2 = f2.readlines()

for ln in contentsf1:
    token1 = ln.split()

for ln2 in contentsf2:
    token2 = ln2.split()
        
if contentsf1 == contentsf2:
    print("Files have the same content")

if contentsf1 != contentsf2:
    print("Files don't have the same content")

我已经尝试了一些东西,尤其是使用 '.startswith("#")' ,但结果一无所获。感谢我能得到的任何帮助。如果这是一个愚蠢的问题,我很抱歉浪费您的时间。

4

0 回答 0