如何让 Python 在程序所在位置查找文件?
我在一个必须加载图像和腌制对象的程序中遇到问题:我的 Python 软件似乎没有在程序的位置中查找。我的程序位于名为“King's Capture”的文件夹中,图像位于“King's Capture”内标有“数据”的文件夹中。我想让 python 找到这些文件,无论我将文件夹“King's Capture”放在哪里。在我看来,python 应该已经在程序本身所在的文件夹中查找,但显然没有。我该怎么办?
I'm having a problem with a program in which I have to load images and pickled objects: my Python software doesn't appear to be looking in the location of the program. I have my program in a folder called "King's Capture," and my images in a folder within "King's Capture" labeled "data." I want to have python find the files no matter where I place the folder "King's Capture." It seems to me that python should already be looking in the folder where the program itself is, but it apparently isn't. How should I go about this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以通过特殊变量
__file__
访问当前脚本文件的路径。因此,请在主程序脚本中尝试以下操作:You can access the path of the current script file via the special variable
__file__
. So try this within your main program script:试试这个
Try this