FileNotFoundError: [Errno 2] No such file or directory – Python

huangapple go评论67阅读模式
英文:

FileNotFoundError: [Errno 2] No such file or directory - Python

问题

抱歉,你遇到这个问题的原因是路径中多了一个反斜杠 \。以下是已修复的路径:

model_dir = os.path.join(currentDir, 'saved_models', model_name, model_name + '.pth')
英文:

I am trying to run this python script I got off the internet but keep getting this error why does it keep adding an extra \ in the path

Sorry I am a newbie to python

FileNotFoundError: [Errno 2] No such file or directory: 'c:\\Users\\HOMEPC\\Downloads\\Image-Remover-Python-main\\saved_models\\u2net\\u2net.pth

Here is the code

import os

currentDir = os.path.dirname(__file__)

print("---Loading Model---")
model_name = 'u2net'
model_dir = os.path.join(currentDir, 'saved_models',
                         model_name, model_name + '.pth')

答案1

得分: 1

from pathlib import Path

currentDir = Path(__file__).parent

print("---Loading Model---")
model_name = 'u2net'
model_file = currentDir / f'saved_models/{model_name}.pth'

I suggest you change model_dir to model_file, because it is not a directory.

Besides, you should make sure there is a directory named saved_models in the current path, which I think is why your code failed.

英文:

you can use pathlib insteam of os.path

from pathlib import Path

currentDir = Path(__file__).parent

print("---Loading Model---")
model_name = 'u2net'
model_dir = currentDir / f'saved_models/{model_name}.pth'

I sugguest you change model_dir to model_file, because it is not a directory.

Besides, you should make sure this is a directory named saved_models in current path, which I think is why you code failed.

huangapple
  • 本文由 发表于 2023年7月20日 15:31:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76727604.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定