Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python equivalent of JS path.resolve()?

I want to do some path resolution in python without assuming file separator char. In JS you use path.resolve(__dirname, '..','xxx','yyy') to get $PWD/../xxx/yyy on *nix machines. Is there an equivalent function in python?

like image 459
Magnus Avatar asked Feb 28 '26 23:02

Magnus


1 Answers

As James Smith pointed out, you can use os.path.join function.

import os

os.path.join(os.getcwd(), '..', 'xxx', 'yyy') # Returns $PWD/../xxx/yyy

It joins one or more path components using the platform specific directory separator. Could be used for both relative and absolute paths.

os.path.join('foo', 'bar') # Returns foo/bar
like image 181
kabirbaidhya Avatar answered Mar 03 '26 14:03

kabirbaidhya