os.path.islink 在 windows 上使用 python [英] os.path.islink on windows with python

查看:36
本文介绍了os.path.islink 在 windows 上使用 python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在带有 Python 2.7 的 Windows 7 上,如何检测路径是否为符号链接?这不起作用 os.path.islink(),它说如果 false 或不支持它返回 false 并且我提供的路径绝对是一个符号链接所以我假设它不支持视窗?我能做什么?

解决方案

根本问题是您使用的 Python 版本太旧.如果您想坚持使用 2.x,您将无法利用 2010 年初之后添加的新功能.

其中一项功能是处理 NTFS 符号链接.该功能于 2010 年底在 3.2 中添加.(参见 3.23.12.7 详细信息来源.)

在那之前 Python 没有处理 NTFS 符号链接的原因是直到 2009 年末才出现这样的事情.(IIRC,支持包含在 6.0 内核中,但用户空间支持需要在 Vista/2008 上安装服务包;只有 7/2008R2 和更新版本内置了它.此外,您需要一个足够新的 MSVCRT 才能访问该用户空间支持,并且 Python 有一个明确的政策,即不在次要版本中升级到新的 Visual Studio 版本.)>

代码没有移植回 2.x 的原因是 永远不会是 2.8,并且像 2.7.3(或 2.7.4)这样的错误修复版本不会获得新功能,只有错误修复.

这已被报告为 issue 13143,预期的修复是更改 2.7 文档以澄清islink 在 Windows 上总是返回 False.

所以,如果你想在 Windows 下读取 NTFS 符号链接,要么升级到 Python 3.2+,要么你必须使用 win32apictypes 等自己做.

或者,正如 Martijn Pieters 建议的那样,不要自己动手,而是使用第三方库,例如 jaraco.windows 这样做和/或借用 他们的代码.

或者,如果你真的想要,从 3.2 源代码中借用代码并围绕它构建一个 C 扩展模块.如果你从 ntpathos 再到 nt(实际上是 posixmodule.c),我相信它的核心在 win32_xstat_impl 和 <代码>win32_xstat_impl_w.

On Windows 7 with Python 2.7 how can I detect if a path is a symbolic link? This does not work os.path.islink(), it says it returns false if false or not supported and the path I'm providing is definitely a symbolic link so I'm assuming it's not supported on windows? What can I do?

解决方案

The root problem is that you're using too old a version of Python. If you want to stick to 2.x, you will not be able to take advantage of new features added after early 2010.

One of those features is handling NTFS symlinks. That functionality was added in 3.2 in late 2010. (See the 3.2, 3.1, and 2.7 source for details.)

The reason Python didn't handle NTFS symlinks before then is that there was no such thing until late 2009. (IIRC, support was included in the 6.0 kernel, but userland support requires a service pack on Vista/2008; only 7/2008R2 and newer come with it built in. Plus, you need a new-enough MSVCRT to be able to access that userland support, and Python has an explicit policy of not upgrading to new Visual Studio versions within a minor release.)

The reason the code wasn't ported back to 2.x is that there will never be a 2.8, and bug fix releases like 2.7.3 (or 2.7.4) don't get new features, only bug fixes.

This has been reported as issue 13143, and the intended fix is to change the 2.7 docs to clarify that islink always returns False on Windows.

So, if you want to read NTFS symlinks under Windows, either upgrade to Python 3.2+, or you have to use win32api, ctypes, etc. to do it yourself.

Or, as Martijn Pieters suggests, instead of doing it yourself, use a third-party library like jaraco.windows that does it and/or borrow their code.

Or, if you really want, borrow the code from the 3.2 source and build a C extension module around it. If you trace down from ntpath to os to nt (which is actually posixmodule.c), I believe the guts of it are in win32_xstat_impl and win32_xstat_impl_w.

这篇关于os.path.islink 在 windows 上使用 python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆