nodejs 从绝对路径获取文件名? [英] nodejs get file name from absolute path?

查看:21
本文介绍了nodejs 从绝对路径获取文件名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何 API 可以从绝对文件路径中检索文件名?

If there any API could retrieve file name from an absolute file path?

例如"foo.txt" 来自 "/var/www/foo.txt"

我知道它适用于字符串操作,比如 fullpath.replace(/.+//, '')但我想知道有没有更正式"的方式,比如 java 中的 file.getName() ,可以做到.

I know it works with string operation, like fullpath.replace(/.+//, '') but I want to know is there a more 'formal' way, like file.getName() in java, could do it.

NodeJS 从绝对路径获取文件名?

NodeJS get file name from absolute path?

推荐答案

使用path模块的basename方法:

path.basename('/foo/bar/baz/asdf/quux.html')
// returns
'quux.html'

这里是上面例子的文档.

这篇关于nodejs 从绝对路径获取文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆