path.normalize(path)
path.normalize() 方法会规范化给定的 path,解析 '..' 和 '.' 段。
🌐 The path.normalize() method normalizes the given path, resolving '..' and
'.' segments.
当发现多个顺序路径段分离特性时(例如: 在 POSIX 上显示“/”,在 Windows 上则为“' 或 ”/'),它们被一个单词替代 平台专用路径段分隔符(POSIX上的“/”)实例 Windows上的“\”)。拖曳分离器得以保存。
🌐 When multiple, sequential path segment separation characters are found (e.g.
/ on POSIX and either \ or / on Windows), they are replaced by a single
instance of the platform-specific path segment separator (/ on POSIX and
\ on Windows). Trailing separators are preserved.
如果 path 是一个长度为零的字符串,将返回 '.',表示当前工作目录。
🌐 If the path is a zero-length string, '.' is returned, representing the
current working directory.
例如,在 POSIX 上:
🌐 For example, on POSIX:
path.normalize('/foo/bar//baz/asdf/quux/..');
// Returns: '/foo/bar/baz/asdf' 在 Windows 上:
🌐 On Windows:
path.normalize('C:\\temp\\\\foo\\bar\\..\\');
// Returns: 'C:\\temp\\foo\\' 由于 Windows 可以识别多种路径分隔符,所有分隔符都将被替换为 Windows 首选的分隔符(\):
🌐 Since Windows recognizes multiple path separators, both separators will be
replaced by instances of the Windows preferred separator (\):
path.win32.normalize('C:////temp\\\\/\\/\\/foo/bar');
// Returns: 'C:\\temp\\foo\\bar' 如果 path 不是字符串,则会抛出 TypeError。
🌐 A TypeError is thrown if path is not a string.