简体   繁体   English

Node.js中的文件系统与路径模块

[英]File System vs. Path module in Node.js

What are the purposes and differences between these two modules in Node.js? Node.js中这两个模块的目的和区别是什么? Does one depend on the other? 一个人依赖另一个吗?

What are the purposes and differences between these two modules in Node.js? Node.js中这两个模块的目的和区别是什么?

The fs module is for actually operating on files, directories and volumes (assuming you have already built an appropriate path for the target). fs模块用于实际操作文件,目录和卷(假设您已经为目标构建了适当的路径)。 The path module is for manipulating paths which you may then use with the fs module since many fs methods accept a path as an argument. path模块用于操作路径,然后您可以将其与fs模块一起使用,因为许多fs方法接受路径作为参数。

The fs module contains functions for manipulating files such as: fs模块包含用于处理文件的函数,例如:

fs.readFile()
fs.mkdir()
fs.open()
fs.stat()

etc... 等等...

The path module contains functions for manipulating file paths such as: path模块包含用于操作文件路径的函数,例如:

path.join()
path.normalize()
path.extname()
path.parse()

You can read the entire list of functions in each module yourself: 您可以自己阅读每个模块中的完整功能列表:

fs module fs模块

path module 路径模块

The descriptions should be pretty obvious what they do. 这些描述应该非常明显。

Does one depend on the other? 一个人依赖另一个吗?

Probably not. 可能不是。 The fs module assumes you already have a valid path that can be passed right on through to the OS. fs模块假设您已经有一个可以直接传递给OS的有效路径。 The path module only builds or parses paths, it doesn't actually do operations on files. path模块仅构建或解析路径,它实际上不对文件执行操作。

It would be very common to use the two together. 将两者结合使用将是非常普遍的。 For example, you might use the path module to construct a path which you then pass to an fs module function. 例如,您可以使用path模块构造一个路径,然后将该路径传递给fs模块函数。

Path module is actually used to construct a valid path from several chunks which never validates in your file system or you can say drives/volumes and gives you an absolute path in string form. 路径模块实际上用于构建来自几个块的有效路径,这些块永远不会在您的文件系统中验证,或者您可以说驱动器/卷并以字符串形式提供绝对路径。 Let say, you have drive, relative pathof a file to that particular drive, filename and extension. 假设您有驱动器,文件的相对路径到该特定驱动器,文件名和扩展名。 In that case you can construct a valid filename by combining it. 在这种情况下,您可以通过组合它来构造有效的文件名。

FS module meant to manipulate the filesystem. FS模块意味着操纵文件系统。 Like creating a directory, file, renaming etc.. 就像创建目录,文件,重命名等。

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

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