简体   繁体   中英

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

Use os.path.join() to join the two path strings together, and os.path.abspath() to resolve the relative parts into one absolute path.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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