简体   繁体   English

添加相对目录并将其更改为现有路径(python 脚本)

[英]Add and change relative directory to an existed path (python script)

There is a default path in a function:函数中有一个默认路径:

def function(directory):
path = ('/a/b/c/d/e/f/g')
newdirectory = (This is the part I am asking)

I call this function:我调用这个函数:

from xxx import function:
p('../../x/y')

I need to get a new directory 'newdirectory', which is supposed to be 'a/b/c/d/e/x/y', in order to proceed to the next step, but I don't know how to add the relative directory to an absolute directory and generate a new absolute directory我需要获取一个新目录“newdirectory”,它应该是“a/b/c/d/e/x/y”,以便进行下一步,但我不知道如何添加相对目录到绝对目录并生成新的绝对目录

Use os.path.join() to join the two path strings together, and os.path.abspath() to resolve the relative parts into one absolute path.使用os.path.join()将两个路径字符串连接在一起,使用os.path.abspath()将相关部分解析为一个绝对路径。

import os
print os.path.abspath(os.path.join('/a/b/c/d/e/f/g', '../../x/y'))

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

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