且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何解决python中找不到文件的问题?

更新时间:2022-03-23 06:12:53

使用 __ file __ 变量,然后获取其父项.

Use the __file__ variable, then get its parent.

示例:

from pathlib import Path

my_dir = Path(__file__).parent

稍后,如果函数了解 Path 对象或直接 str(),则可以直接使用 my_dir :

Later on, you can use my_dir directly if the function understand Path objects, or str() it first:

# if root.iconbitmap accepts Path objects
root.iconbitmap(my_dir / "img/***_(1).ico")

#or

# if root.iconbitmap does not accept Path objects
icopath = str(my_dir / "img/***_(1).ico")
root.iconbitmap(icopath)