简体   繁体   English

Python 相当于 JS path.resolve()?

[英]Python equivalent of JS path.resolve()?

I want to do some path resolution in python without assuming file separator char.我想在 python 中做一些路径解析而不假设文件分隔符。 In JS you use path.resolve(__dirname, '..','xxx','yyy') to get $PWD/../xxx/yyy on *nix machines.在 JS 中,您使用path.resolve(__dirname, '..','xxx','yyy')在 *nix 机器上获取$PWD/../xxx/yyy Is there an equivalent function in python? python中有等效的函数吗?

As James Smith pointed out, you can use os.path.join function.正如詹姆斯史密斯指出的那样,您可以使用os.path.join函数。

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

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

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