为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

海绵宝宝撒 2022-06-07 19:32:24
为什么 f1= open(filethub) 在几次成功迭代后仍然失败?import stringimport shutilimport diffliboldPath = 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 是用户输入值,例如
查看完整描述

3 回答

?
慕斯王

TA贡献1864条经验 获得超2个赞

我注意到上面的评论并想注意你不会用 print() 捕捉到 None 类型:


这样做:


>a = None

>print(a)

将输出None到控制台。


我会遵循给出的尝试捕捉错误的建议。你甚至可以做一个简单的检查。


代替:


f1 = open(filethub)

你可以这样做:


if filethub:

    f1 = open(filethub)

但是对整个底部部分进行尝试捕获可能会更好,并且它几乎必须遍历脚本的整个下部。您甚至可以记录错误文件以供以后查看。


查看完整回答
反对 回复 2022-06-07
?
忽然笑

TA贡献1806条经验 获得超5个赞

似乎错误在返回 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):

  File "./line_diff.py", line 90, in <module>

    main()

  File "./line_diff.py", line 34, in main

    f1 = open(filethub)

TypeError: expected str, bytes or os.PathLike object, not NoneType


查看完整回答
反对 回复 2022-06-07
?
一只甜甜圈

TA贡献1836条经验 获得超5个赞

  1. 错误消息告诉我们open使用参数调用None

  2. 参数来自filethub,所以filethubNone

  3. 最后一次filethub分配是由find函数分配的。

  4. 所以,find函数返回None

你没有包含这个find功能,所以我不能说为什么。但是您可能需要检查是否find返回None,如果是,则继续下一次迭代。


查看完整回答
反对 回复 2022-06-07
  • 3 回答
  • 0 关注
  • 530 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号