In my File-system my working directory is here:
在我的文件系统中,我的工作目录在这里:
C:\temp\a\b\c\d
C:\ temp \ \ b \ C \ d
and under b\bb there's file: tmp.txt
在b\bb下有一个文件:tmp.txt
C:\temp\a\b\bb\tmp.txt
C:\ temp \ \ b \ bb \ tmp.txt
If I want to go to this file from my working directory, I'll use this path:
如果我想从我的工作目录到这个文件,我将使用这条路径:
"../../bb/tmp.txt"
In case the file is not exist I want to log the full path and tell the user:
"The file C:\temp\a\b\bb\tmp.txt is not exist".
如果文件不存在,我想记录完整的路径并告诉用户:“文件C:\temp\a\b\b \bb\tmp”。三是不存在的”。
My question:
我的问题:
I need some function that convert the relative path: "../../bb/tmp.txt" to absolute: "C:\temp\a\b\bb\tmp.txt"
我需要一些函数来转换相对路径:../../bb/tmp。txt”绝对:“C:\ temp \ \ b \ bb \ tmp.txt”
In my code it should be like this:
在我的代码中应该是这样的:
console.log("The file" + convertToAbs("../../bb/tmp.txt") + " is not exist")
2 个解决方案
#1
51
Use path.resolve
使用path.resolve
try:
试一试:
resolve = require('path').resolve
resolve('../../bb/tmp.txt')
#2
1
You could also use __dirname and __filename for absolute path.
您还可以使用__dirname和__filename作为绝对路径。
#1
51
Use path.resolve
使用path.resolve
try:
试一试:
resolve = require('path').resolve
resolve('../../bb/tmp.txt')
#2
1
You could also use __dirname and __filename for absolute path.
您还可以使用__dirname和__filename作为绝对路径。