简体   繁体   English

如何通过相对路径获取“其他”项目文件的绝对路径?

[英]How to obtain absolute path via relative path for 'other' project files?

How to obtain absolute path via relative path for 'other' project files, not those python file in the project, like Java? 如何通过相对路径获取“其他”项目文件的绝对路径,而不是像Java这样的项目中的python文件?

D:\Workspaces\ABCPythonProject\

|- src
|   |-- com/abc
|       |-- conf.py
|       |-- abcd.py
|       |-- defg.py
|       |-- installation.rst
|- resources
|   |-- a.txt
|   |-- b.txt
|   |-- c.jpg

For example, I would like access 'a.txt' or 'b.txt' in python codes like 'abcd.py' in a simple manner with variable like 'resource/a.txt', just like a Java project in Java. 例如,我想以简单的方式使用诸如“ resource / a.txt”之类的变量,以“ abcd.py”之类的python代码访问“ a.txt”或“ b.txt”,就像Java中的Java项目一样。

In short, I want to get ' D:\\Workspaces\\ABCPythonProject\\resources\\a.txt ' by ' resources\\a.txt ', which is extremely easy to do in Java, but is seemingly extremely difficult to achieve in Python. 简而言之,我想通过“ resources \\ a.txt ”获得“ D:\\ Workspaces \\ ABCPythonProject \\ resources \\ a.txt ”,这在Java中非常容易实现,但是在Python中似乎很难实现。

(If I use the built-in python methods like 'os.filePath.join(os.filePath.dirname(_file__), 'resources/a.txt')', os.path.dirname('resources/a.txt'), os.path.abspath('resources/a.txt'), ..., etc., the results is always " D:\\Workspaces\\ABCPythonProject\\com\\abc\\resources\\a.txt ", a non-exit file path. ) (如果我使用内置的python方法,例如'os.filePath.join(os.filePath.dirname(_file__),'resources / a.txt')',os.path.dirname('resources / a.txt' ),os.path.abspath('resources / a.txt')等...,结果始终为“ D:\\ Workspaces \\ ABCPythonProject \\ com \\ abc \\ resources \\ a.txt ”,非退出文件路径。)

How to achieve this? 如何实现呢?

You can use those built-in python methods, but if you call your script in ../src/com/abc. 您可以使用那些内置的python方法,但是如果您在../src/com/abc中调用脚本。 Your os.filePath() function will execute in your current directory. 您的os.filePath()函数将在当前目录中执行。 Your current directory is your execution directory (directory of Python script!!) At first you should change directory to access your .txt-file, because python does not execute in your "ABCPythonProject" directory. 当前目录是您的执行目录(Python脚本的目录!)。首先,您应该更改目录以访问.txt文件,因为python不在您的“ ABCPythonProject”目录中执行。 Obviously you can not access to recourses/a.txt this way, because you have to change to directory "ABCPythonProject" to check this path. 显然,您不能以这种方式访问​​recourses / a.txt,因为您必须切换到目录“ ABCPythonProject”以检查此路径。

os.chdir("D:\Workspaces\ABCPythonProject")

You can check your directory, you are currently in with: 您可以检查目录,目前位于:

print (os.getcwd())

This sample might benefit from a little cleaning, but it does the job. 该样品可能会受益于少量清洁,但可以完成工作。

import os
k = os.path.abspath(os.path.join(".", os.pardir)) # one level up
print k
k = os.path.abspath(os.path.join(k, os.pardir))   # two levels up
print k
k = os.path.abspath(os.path.join(k, 'resources/a.txt'))   # two levels down
print k  

Starting in directory C:\\Users\\Philip\\AppData\\Local\\Temp\\src\\com output is 从目录C:\\Users\\Philip\\AppData\\Local\\Temp\\src\\com输出为

C:\Users\Philip\AppData\Local\Temp\src
C:\Users\Philip\AppData\Local\Temp
C:\Users\Philip\AppData\Local\Temp\resources\a.txt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM