为什么 f1 = open(filename) 不断出错?这里有限制吗?

为什么 f1= open(filethub) 在几次成功迭代后仍然失败?


import string

import shutil

import difflib


oldPath = input("What is the old directory?")

thubPath = input("What is the thub directory?")

toFile = input("What directory do you want the output file to go to? ")





def find(name, path):

    for root, dirs, files in os.walk(path):

        if name in files:

            tf = os.path.join(root, name)

            return tf 




def main():



    with open('difftext.txt', 'a')as outfile:


        for root, dirs, files in os.walk(oldPath):

            for file in files:

                fileold = file

                filethub = find(fileold, thubPath)

                fileoldn = os.path.join(root,fileold)

                print(filethub)

                f1 = open(filethub)

                f2 = open(fileoldn)

                outfile.write("|||||||||||||||||||||||||||||||||||\n")

                outfile.write("\nComparing files \n")

                outfile.write("-----------------------------------\n")

                outfile.write(" > " + str(os.path.basename(filethub))+'\n')

                outfile.write(" < " +str(os.path.basename(fileoldn))+'\n')

                outfile.write("-----------------------------------\n")

                # Read the first line from the files

                f1_line = f1.readline()

                f2_line = f2.readline()


                # Initialize counter for line number

                line_no = 1


                # Loop if either file1 or file2 has not reached EOF

                while f1_line != '' or f2_line != '':


                    # Strip the leading whitespaces

                    f1_line = f1_line.rstrip()

                    f2_line = f2_line.rstrip()


                    # Compare the lines from both file

                    if f1_line != f2_line:



创建此脚本以遍历包含相同文件的两个版本的两个目录,并在单个文件中打印出差异。oldPath 和 thubpath 是用户输入值,例如



海绵宝宝撒
浏览 531回答 3
3回答

慕斯王

我注意到上面的评论并想注意你不会用 print() 捕捉到 None 类型:这样做:>a = None>print(a)将输出None到控制台。我会遵循给出的尝试捕捉错误的建议。你甚至可以做一个简单的检查。代替:f1 = open(filethub)你可以这样做:if filethub:&nbsp; &nbsp; f1 = open(filethub)但是对整个底部部分进行尝试捕获可能会更好,并且它几乎必须遍历脚本的整个下部。您甚至可以记录错误文件以供以后查看。

忽然笑

似乎错误在返回 NoneType 的 find 函数中,但我不知道为什么通过在 find 函数中添加print(type(filethub))打开文件和打印语句之前找到。<class 'str'><class 'str'><class 'str'><class 'str'><class 'str'><class 'str'><class 'str'><class 'str'><class 'str'><class 'str'><class 'str'><class 'str'><class 'str'><class 'str'><class 'str'><class 'str'><class 'str'>heres your problem<class 'NoneType'>Traceback (most recent call last):&nbsp; File "./line_diff.py", line 90, in <module>&nbsp; &nbsp; main()&nbsp; File "./line_diff.py", line 34, in main&nbsp; &nbsp; f1 = open(filethub)TypeError: expected str, bytes or os.PathLike object, not NoneType

一只甜甜圈

错误消息告诉我们open使用参数调用None。参数来自filethub,所以filethub是None。最后一次filethub分配是由find函数分配的。所以,find函数返回None。你没有包含这个find功能,所以我不能说为什么。但是您可能需要检查是否find返回None,如果是,则继续下一次迭代。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python