如何解析python中的相对路径?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32838760/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 12:22:31  来源:igfitidea点击:

How to resolve relative paths in python?

pythonpath

提问by niyant

I have Directory structure like this

我有这样的目录结构

projectfolder/fold1/fold2/fold3/script.py

now I'm giving script.py a path as commandline argument of a file which is there in

现在我给 script.py 一个路径作为一个文件的命令行参数

fold1/fold_temp/myfile.txt

So basically I want to be able to give path in this way

所以基本上我希望能够以这种方式给出路径

../../fold_temp/myfile.txt 

>>python somepath/pythonfile.py -input ../../fold_temp/myfile.txt

Here problem is that I might be given full path or relative path so I should be able to decide and based on that I should be able to create absolute path.

这里的问题是我可能会得到完整路径或相对路径,所以我应该能够决定并基于我应该能够创建绝对路径。

I already have knowledge of functions related to path.

我已经了解了与路径相关的功能。

Question 1

问题 1

Question 2

问题2

Reference questions are giving partial answer but I don't know how to build full path using the functions provided in them.

参考问题给出了部分答案,但我不知道如何使用其中提供的函数构建完整路径。

采纳答案by Hooting

import os
dir = os.path.dirname(__file__)
path = raw_input()
if os.path.isabs(path):
    print "input path is absolute"
else:
    path = os.path.join(dir, path)
    print "absolute path is %s" % path

Use os.path.isabs to judge if input path is absolute or relative, if it is relative, then use os.path.join to convert it to absolute

使用 os.path.isabs 判断输入路径是绝对路径还是相对路径,如果是相对路径,则使用 os.path.join 转换为绝对路径

回答by Salo

try os.path.abspath, it should do what you want ;)

试试 os.path.abspath,它应该做你想做的;)

Basically it converts any given path to an absolute path you can work with, so you do not need to distinguish between relative and absolute paths, just normalize any of them with this function.

基本上它将任何给定的路径转换为您可以使用的绝对路径,因此您无需区分相对路径和绝对路径,只需使用此函数将它们中的任何一个标准化即可。

Example:

例子:

from os.path import abspath
filename = abspath('../../fold_temp/myfile.txt')
print(filename)

It will output the absolute path to your file.

它将输出文件的绝对路径。

EDIT:

编辑:

If you are using Python 3.4 or newer you may also use the resolve()method of pathlib.Path. Be aware that this will return a Path object and not a string. If you need a string you can still use str()to convert it to a string.

如果您使用 Python 3.4 或更新版本,您还可以使用pathlib.Pathresolve()方法。请注意,这将返回一个 Path 对象而不是一个字符串。如果您需要一个字符串,您仍然可以使用将其转换为字符串。str()

Example:

例子:

from pathlib import Path
filename = Path('../../fold_temp/myfile.txt').resolve()
print(filename)

回答by Bryce Guinta

For Python3, you can use pathlib's resolvefunctionality to resolve symlinks and ..components.

对于 Python3,您可以使用 pathlib 的解析功能来解析符号链接和..组件。

You need to have a Path object however it is very simple to do convert between str and Path.

您需要有一个 Path 对象,但是在 str 和 Path 之间进行转换非常简单。

I recommend for anyone using Python3 to drop os.pathand its messy long function names and stick to pathlibPath objects.

我建议任何使用 Python3 的人删除os.path其杂乱的长函数名并坚持使用pathlibPath 对象。

回答by Lucas Azevedo

A practical example:

一个实际例子:

sys.argv[0]gives you the name of the current script

sys.argv[0]给你当前脚本的名称

os.path.dirname()gives you the relative directory name

os.path.dirname()给你相对目录名

thus, the next line, gives you the absolute working directory of the current executing file.

因此,下一行为您提供当前执行文件的绝对工作目录。

cwd = os.path.abspath(os.path.dirname(sys.argv[0]))

cwd = os.path.abspath(os.path.dirname(sys.argv[0]))

Personally, I always use this instead of os.getcwd()since it gives me the script absolute path, independent of the directory from where the script was called.

就个人而言,我总是使用它而不是os.getcwd()因为它为我提供了脚本绝对路径,独立于调用脚本的目录。