python - 为什么在 Windows 上,python3,os.path.abspath 不以相同的方式处理前导斜杠,如果它只是一个 dir 或更多?
问题描述
在 Windows 上,python3:
>>> print(os.path.abspath("//foo/foo.txt"))
\\foo\foo.txt
>>> print(os.path.abspath("//foo"))
\foo
在 python2 上:
>>> print(os.path.abspath("//foo/foo.txt"))
\\foo\foo.txt
>>> print(os.path.abspath("//foo"))
\\foo
为什么会这样?
鉴于我必须将路径一起比较,有些就像第一个例子,有些像第二个例子,你会如何处理这个问题?
我必须找到的唯一可怕的方法是:
In [34]: re.match(r"^(//|\\\\)(?!.+(/|\\))", "//foo")
Out[34]: <re.Match object; span=(0, 2), match='//'>
In [35]: re.match(r"^(//|\\\\)(?!.+(/|\\))", "\\\\foo")
Out[35]: <re.Match object; span=(0, 2), match='\\\\'>
In [36]: re.match(r"^(//|\\\\)(?!.+(/|\\))", "//foo/bar")
In [37]: re.match(r"^(//|\\\\)(?!.+(/|\\))", "\\\\foo\\bar")
所以我最终不得不做类似的事情:
file_path = "//foo"
match = False
if re.match(r"^(//|\\\\)(?!.+(/|\\))", file_path):
match = True
file_path = os.path.abspath(file_path)
if match:
file_path = file_path.replace("\\", "\\\\")
解决方案
推荐阅读
- javascript - 如何在 Handlebars 模板中包含脚本元素?
- r - 在 R 中限制 ggplot2 x 轴大小
- html - 我可以使用 CSS 将元素与特定浏览器相关联吗
- sql - 从一张桌子上抓取所有取消的客户,并在另一张桌子上计算他们的所有订单
- html - Send-MailMessage 使用 HTML 文件作为正文将单引号和破折号更改为问号
- c# - 如何使用自定义 xamarin.forms 库
- arrays - SwiftUI - 删除时列表中绑定数组的有趣问题
- javascript - 将此迭代函数转换为递归
- jquery - 如何在传单多边形上没有鼠标悬停的情况下显示标签?
- javascript - 移动应用 浏览器 录音机 高频