亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么 f1 = open(filename) 不斷出錯?這里有限制嗎?

為什么 f1 = open(filename) 不斷出錯?這里有限制嗎?

海綿寶寶撒 2022-06-07 19:32:24
為什么 f1= open(filethub) 在幾次成功迭代后仍然失?。縤mport 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 關注
  • 508 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號