![](/img/trans.png)
[英]Python: FileNotFoundError: [WinError 3] The system cannot find the path specified: ''
[英]Python [WinError 3] The system cannot find the path specified
起初這個腳本運行正常但在它顯示這個錯誤"[WinError 3] The system cannot find the path specified"
之后沒有改變腳本中的任何東西
import os
paths = os.listdir(r'C:\Users\Film\OneDrive\Documents\WORK\Blockfint\Richy_csv_files\Recovery_as_compu_11_14_2022_14_9_32\Tables')
def files_with_word(word:str, paths:list) -> str:
for path in paths:
with open(path, "r") as f:
if word in f.read():
yield path
for filepath in files_with_word("Admin", paths):
print(filepath)
我嘗試卸載所有 python 並使用python 3.11 64 bit
重新安裝它仍然無法正常工作
您遇到的問題看起來像是沒有使用絕對路徑。 paths = os.listdir(r'C:\Users\Film\OneDrive\Documents\WORK\Blockfint\Richy_csv_files\Recovery_as_compu_11_14_2022_14_9_32\Tables')
只會得到一個沒有路徑信息的文件名列表。 因此,如果您實際上並未在同一目錄中運行 python 文件,則會產生該文件未找到的錯誤。
在 for 循環中,我只是將源目錄和文件名放在一起以獲得完整的打開路徑。 您還需要過濾掉目錄,因為當前代碼還會嘗試將目錄作為文件打開並導致錯誤。
import os
src = r'C:\Users\Film\OneDrive\Documents\WORK\Blockfint\Richy_csv_files\Recovery_as_compu_11_14_2022_14_9_32\Tables'
files = os.listdir(src)
# only get files. filter out directories
files = [f for f in files if os.path.isfile(src+'/'+f)]
def files_with_word(word:str, files:list) -> str:
for file in files:
# create full path to file
full_path = src + "\\" + file
#open using full path
print(full_path)
with open(full_path, "r") as f:
if word in f.read():
yield file
for filepath in files_with_word("Admin", files):
print(filepath)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.