如何可靠地打开与当前正在运行的脚本位于同一目录中的文件 [英] How to reliably open a file in the same directory as the currently running script

查看:13
本文介绍了如何可靠地打开与当前正在运行的脚本位于同一目录中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾经通过简单地使用如下命令来打开与当前运行的 Python 脚本位于同一目录中的文件:

I used to open files that were in the same directory as the currently running Python script by simply using a command like:

open("Some file.txt", "r")

然而,我发现当脚本在 Windows 中通过双击运行时,它会尝试从错误的目录打开文件.

However, I discovered that when the script was run in Windows by double-clicking it, it would try to open the file from the wrong directory.

从那以后我使用了一个命令形式

Since then I've used a command of the form

open(os.path.join(sys.path[0], "Some file.txt"), "r")

每当我想打开文件时.这适用于我的特定用法,但我不确定 sys.path[0] 在其他用例中是否会失败.

whenever I wanted to open a file. This works for my particular usage, but I'm not sure if sys.path[0] might fail in some other use case.

所以我的问题是:打开与当前运行的 Python 脚本位于同一目录中的文件的最佳和最可靠的方法是什么?

So my question is: What is the best and most reliable way to open a file that's in the same directory as the currently running Python script?

以下是我目前能够弄清楚的:

Here's what I've been able to figure out so far:

  • os.getcwd()os.path.abspath('') 返回当前工作目录",而不是脚本目录.

  • os.getcwd() and os.path.abspath('') return the "current working directory", not the script directory.

os.path.dirname(sys.argv[0])os.path.dirname(__file__) 返回用于调用脚本的路径,这可能是相对的,甚至是空白的(如果脚本在 cwd 中).此外,当脚本在 IDLE 或 PythonWin 中运行时,__file__ 不存在.

os.path.dirname(sys.argv[0]) and os.path.dirname(__file__) return the path used to call the script, which may be relative or even blank (if the script is in the cwd). Also, __file__ does not exist when the script is run in IDLE or PythonWin.

sys.path[0]os.path.abspath(os.path.dirname(sys.argv[0])) 似乎返回脚本目录.我不确定这两者之间是否有任何区别.

sys.path[0] and os.path.abspath(os.path.dirname(sys.argv[0])) seem to return the script directory. I'm not sure if there's any difference between these two.

我刚刚意识到我想要做的最好描述为在与包含模块相同的目录中打开一个文件".换句话说,如果我导入我在另一个目录中编写的模块,并且该模块打开一个文件,我希望它在模块目录中查找该文件.我认为我发现的任何东西都无法做到这一点......

I just realized that what I want to do would be better described as "open a file in the same directory as the containing module". In other words, if I import a module I wrote that's in another directory, and that module opens a file, I want it to look for the file in the module's directory. I don't think anything I've found is able to do that...

推荐答案

我一直使用:

__location__ = os.path.realpath(
    os.path.join(os.getcwd(), os.path.dirname(__file__)))

join() 调用将当前工作目录放在前面,但文档说如果某个路径是绝对路径,则其左侧的所有其他路径都将被删除.因此,当 dirname(__file__) 返回绝对路径时,会删除 getcwd().

The join() call prepends the current working directory, but the documentation says that if some path is absolute, all other paths left of it are dropped. Therefore, getcwd() is dropped when dirname(__file__) returns an absolute path.

此外,如果找到符号链接,realpath 调用会解析符号链接.这避免了在 Linux 系统上使用 setuptools 部署时的麻烦(脚本被符号链接到 /usr/bin/ -- 至少在 Debian 上).

Also, the realpath call resolves symbolic links if any are found. This avoids troubles when deploying with setuptools on Linux systems (scripts are symlinked to /usr/bin/ -- at least on Debian).

您可以使用以下命令打开同一文件夹中的文件:

You may the use the following to open up files in the same folder:

f = open(os.path.join(__location__, 'bundled-resource.jpg'))
# ...

我用它来将资源与 Windows 和 Linux 上的几个 Django 应用程序捆绑在一起,它的效果非常棒!

I use this to bundle resources with several Django application on both Windows and Linux and it works like a charm!

这篇关于如何可靠地打开与当前正在运行的脚本位于同一目录中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆