如何在与Python脚本相同的目录中可靠地打开文件 [英] How to reliably open a file in the same directory as a Python script

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

问题描述

我曾经通过简单的使用像

 这样的命令来打开与当前运行的Python脚本在同一目录中的文件打开(一些file.txt,r)

但是,我发现,当脚本通过双击它在Windows中运行,它将尝试从错误的目录中打开文件。



从那时起,我使用了一个命令, p>

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

每当我想打开一个文件。这适用于我的特定用法,但是我不确定如果 sys.path [0] 可能会在某些其他用例中失败。



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



以下是我迄今为止所能确定的内容:




  • os.getcwd() / code>和 os.path.abspath('')返回当前工作目录,而不是脚本目录。


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


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




编辑: / p>

我只是意识到我想做的更好的描述是打开与包含模块相同的目录中的文件。换句话说,如果我导入一个我在另一个目录中写的模块,并且该模块打开一个文件,我希望它在模块的目录中查找文件。我不认为任何我发现能够做到这一点...

解决方案

我总是使用:

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

join()调用前面加上当前的工作目录,但文档说如果一些路径是绝对的,那么剩下的所有其他路径将被丢弃。因此,当 dirname(__ file __)返回绝对路径时, getcwd()将被删除。



另外, realpath 调用解析符号链接(如果有)。这可以避免在Linux系统上使用setuptools进行部署时遇到麻烦(脚本符号链接到 / usr / bin / - 至少在Debian上)



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

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

我用这个来捆绑两个Windows上的几个Django应用程序的资源和Linux,它的作用就像一个魅力!


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")

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")

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.

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() and os.path.abspath('') return the "current working directory", not the script directory.

  • 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] 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.

Edit:

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...

解决方案

I always use:

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

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.

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'));
# ...

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

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

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