英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论