[英]How can I access files in resource directory of a PyDev project without having to give absolute path?
I have a PyDev project in eclipse consisting of a "src" directory and an "rsc" directory. 我在Eclipse中有一个PyDev项目,其中包括“ src”目录和“ rsc”目录。
I would like to read/write files in the "rsc" dir. 我想在“ rsc”目录中读取/写入文件。 If i give for example the following command in .py file in the "src" dir:
例如,如果我在“ src”目录中的.py文件中给出以下命令:
numpy.savetxt("rsc/test.txt", temp, fmt='%3.15f', delimiter=' ')
I get an error saying "No such file: rsc/test.txt", (Giving the absolute path (ie "home/.../test.txt") works.) 我收到一条错误消息:“没有这样的文件:rsc / test.txt”,(给出绝对路径(即“ home /.../ test.txt”)是可行的。)
This works for java projects. 这适用于Java项目。 How can I do this for python projects?
如何为python项目执行此操作? Is this problem specific to eclipse?
这个问题特定于日食吗?
To clarify, my dir structure is as follows: proj_dir -> src -> file.py, proj_dir -> rsc -> test.txt I am running a file in src eg "file.py" 为了澄清,我的目录结构如下:proj_dir-> src-> file.py,proj_dir-> rsc-> test.txt我正在src中运行文件,例如“ file.py”
instead of using : 而不是使用:
numpy.savetxt("rsc/test.txt", temp, fmt='%3.15f', delimiter=' ')
you can use : 您可以使用 :
import os, inspect
this_file = os.path.abspath(inspect.getfile(inspect.currentframe()))
project_dir = os.path.dirname(os.path.dirname(this_file))
numpy.savetext(os.path.join(project_dir,"rsc/test.txt"), temp, fmt='%3.15f', delimiter=' ')
This will always work if your src and rsc directories share the same parent. 如果您的src和rsc目录共享同一个父目录,则这将始终有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.