Javascript fs:如何找到父文件夹?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7083045/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
fs: how do I locate a parent folder?
提问by fancy
How do I write this to go back up the parent 2 levels to find a file?
我如何编写它以返回父级 2 级以查找文件?
fs.readFile(__dirname + 'foo.bar');
回答by Andrew Hare
Try this:
尝试这个:
fs.readFile(__dirname + '/../../foo.bar');
Note the forward slash at the beginning of the relative path.
请注意相对路径开头的正斜杠。
回答by Alex Wayne
Use path.join http://nodejs.org/docs/v0.4.10/api/path.html#path.join
使用 path.join http://nodejs.org/docs/v0.4.10/api/path.html#path.join
var path = require("path"),
fs = require("fs");
fs.readFile(path.join(__dirname, '..', '..', 'foo.bar'));
path.join()
will handle leading/trailing slashes for you and just do the right thing and you don't have to try to remember when trailing slashes exist and when they dont.
path.join()
将为您处理前导/尾随斜杠,并做正确的事情,您不必尝试记住尾随斜杠何时存在以及何时不存在。
回答by smremde
I know it is a bit picky, but all the answers so far are not quite right.
我知道这有点挑剔,但到目前为止所有的答案都不完全正确。
The point of path.join() is to eliminate the need for the caller to know which directory separator to use (making code platform agnostic).
path.join() 的目的是消除调用者知道要使用哪个目录分隔符的需要(使代码平台不可知)。
Technically the correct answer would be something like:
从技术上讲,正确的答案是这样的:
var path = require("path");
fs.readFile(path.join(__dirname, '..', '..', 'foo.bar'));
I would have added this as a comment to Alex Wayne's answer but not enough rep yet!
我会将此添加为对 Alex Wayne 的回答的评论,但还没有足够的代表!
EDIT: as per user1767586's observation
编辑:根据 user1767586 的观察
回答by Yan Foto
The easiest way would be to use path.resolve
:
最简单的方法是使用path.resolve
:
path.resolve(__dirname, '..', '..');
回答by Dominic Barnes
Looks like you'll need the path
module. (path.normalize
in particular)
看起来你需要这个path
模块。(path.normalize
特别是)
var path = require("path"),
fs = require("fs");
fs.readFile(path.normalize(__dirname + "/../../foo.bar"));
回答by Jeremy Battle
If another module calls yours and you'd still like to know the location of the main file being run you can use a modification of @Jason's code:
如果另一个模块调用你的模块并且你仍然想知道正在运行的主文件的位置,你可以使用@Jason 代码的修改:
var path = require('path'),
__parentDir = path.dirname(process.mainModule.filename);
fs.readFile(__parentDir + '/foo.bar');
That way you'll get the location of the script actually being run.
这样,您将获得实际运行的脚本的位置。
回答by Jason Brumwell
If you not positive on where the parent is, this will get you the path;
如果您对父母的位置不满意,这将为您提供路径;
var path = require('path'),
__parentDir = path.dirname(module.parent.filename);
fs.readFile(__parentDir + '/foo.bar');
回答by puneet
You can use
您可以使用
path.join(__dirname, '../..');
回答by グエントゥアンズン
i'm running electron app and i can get the parent folder by path.resolve()
我正在运行电子应用程序,我可以通过 path.resolve() 获取父文件夹
parent 1 level:path.resolve(__dirname, '..') + '/'
父母 1 级:path.resolve(__dirname, '..') + '/'
parent 2 levels:path.resolve(__dirname, '..', '..') + '/'
父级 2 个级别:path.resolve(__dirname, '..', '..') + '/'
回答by Dhruvin modi
this will also work:
这也将起作用:
fs.readFile(`${__dirname}/../../foo.bar`);