We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
// path/to/src/cwd.cjs // 进程运行目录 console.log('process.cwd():', process.cwd()) // 文件所在目录 console.log('__dirname:', __dirname) // path/to/src // 文件绝对路径 console.log('__filename:', __filename) // path/to/src/cwd.cjs
在 Windows 与其他系统执行结果有差异,可通过 path.win32、path.posix 指定系统返回结果
path.win32
path.posix
__dirname: H:\git\nodejs-demo\files\src __filename: H:\git\nodejs-demo\files\src\paths.js path.basename(__filename): paths.js path.basename(__filename, '.js'): paths path.basename(__dirname): src path.basename(__dirname, 'c'): sr // 字符串处理 path.delimiter: ; path.win32.delimiter: ; // 指定Windows平台 path.posix.delimiter: : path.sep: \ path.win32.sep: \ path.posix.sep: / path.dirname(__filename): H:\git\nodejs-demo\files\src path.dirname(__dirname): H:\git\nodejs-demo\files // 返回输入路径的所在目录 path.extname(__filename): .js // 返回最后.及之后的字符串 path.parse(__filename): { root: 'H:\\', dir: 'H:\\git\\nodejs-demo\\files\\src', base: 'paths.js', ext: '.js', name: 'paths' } path.format({root: '/', name: 'file', ext: '.js'}): /file.js path.format({root: '/', base: 'file.js', ext: 'anything, ignored'}): /file.js path.isAbsolute('/baz/..'): true path.isAbsolute('C:/foo/..'): true path.isAbsolute('qux/'): false path.isAbsolute('.'): false path.join('/foo', 'bar', 'baz/asdf/', './abc') \foo\bar\baz\asdf\abc // 将输入字符拼接为有效的路径格式 path.join('/foo', 'bar', 'baz/asdf/', './abc', '..') \foo\bar\baz\asdf path.normalize('/foo/./asdf//////quux/../..') \foo // 将输入字符序列为有效的路径格式 path.relative('/data/test/from', '/data/impl/to') ..\..\impl\to path.resolve('./', 'a/b') H:\git\nodejs-demo\files\a\b // 类似path.join,返回绝对路径 path.toNamespacedPath('./abc') \\?\H:\git\nodejs-demo\files\abc //仅Windows有效 path.win32.toNamespacedPath('./abc') \\?\H:\git\nodejs-demo\files\abc path.posix.toNamespacedPath('./abc') ./abc
CURD
创建文件有三种方法
fs.appendFile( filePath, data[, options], callback )
fs.open( filePath, flag[, mode], callback )
a
a+
w
w+
fs.writeFile( filePath, data[, options] )
fs.createWriteStream(path[, options])
修改文件有两种方法
fs.appendFile()
fs.writeFile()
读取文件有两种方法
fs.open()
fs.readFile(path[, options], callback)
fs.createReadStream(path[, options])
删除文件有两种方法
fs.unlink(path, callback)
fs.rm(path[, options], callback)
The text was updated successfully, but these errors were encountered:
No branches or pull requests
process.cwd()、__dirname
path 模块
在 Windows 与其他系统执行结果有差异,可通过
path.win32
、path.posix
指定系统返回结果fs 模块
CURD
Create
创建文件有三种方法
fs.appendFile( filePath, data[, options], callback )
将指定的内容追加到文件中。文件不存在则创建
fs.open( filePath, flag[, mode], callback )
按标志打开文件。当标志为
a
、a+
、w
、w+
时,文件不存在则创建fs.writeFile( filePath, data[, options] )
替换或创建指定的文件和内容
fs.createWriteStream(path[, options])
Update
修改文件有两种方法
fs.appendFile()
fs.writeFile()
Read
读取文件有两种方法
fs.open()
fs.readFile(path[, options], callback)
fs.createReadStream(path[, options])
Delete
删除文件有两种方法
fs.unlink(path, callback)
删除文件或软链接
fs.rm(path[, options], callback)
删除文件或目录
The text was updated successfully, but these errors were encountered: