Windows 上的符号链接?
有谁知道如何从 Python 跨 win32 版本创建/读取符号链接?理想情况下,应该有最少量的平台特定代码,因为我需要我的应用程序是跨平台的。
Does anyone know of a way to make/read symbolic links across versions of win32 from Python? Ideally there should be a minimum amount of platform specific code, as I need my app to be cross platform.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
NTFS 文件系统有连接点,我想你可以使用它们,你可以使用 python win32 API 模块,例如
如果你不想依赖 win32API 模块,你可以随时使用
ctypes
并直接调用CreateSymbolicLink
win32 API 例如MSDN ( http://msdn.microsoft.com/en-us/library/aa363866(VS.85).aspx) 表示最低支持的客户端是 Windows Vista
此外:这也适用于目录(用第三个参数表示)。有了 unicode 支持,它看起来像这样:
另请参阅
在 Python 中创建 NTFS 连接点
NTFS file system has junction points, I think you may use them instead, You can use python win32 API module for that e.g.
If you do not want to rely on win32API module, you can always use
ctypes
and directly callCreateSymbolicLink
win32 API e.g.MSDN (http://msdn.microsoft.com/en-us/library/aa363866(VS.85).aspx) says Minimum supported client is Windows Vista
In addition: This also works with directories (indicate that with the third argument). With unicode support it looks like this:
also see
Create NTFS junction point in Python
os.symlink 适用于使用 Windows 8.1 和 NTFS 文件系统的 Python 3.3。
os.symlink works on Python 3.3 using Windows 8.1 with an NTFS filesystem.
在子进程中使用 mklink 命令创建链接。
Using mklink command in subprocess create link.
python ntfslink扩展
或者如果你想使用pywin32,你可以使用前面所说的方法,阅读时使用:
Adjust CreateFile 中的属性可以满足您的需要,但对于正常情况,它应该可以工作。请随意改进它。
如果您使用 MOUNTPOINT 而不是 SYMBOLIC_LINK,它也应该适用于文件夹连接。
您可以检查一下是否
将其放入要发布的内容中,因为这种形式的符号链接仅在 Vista+ 上受支持。
python ntfslink extension
Or if you want to use pywin32, you can use the previously stated method, and to read, use:
Adjust the attributes in the CreateFile to your needs, but for a normal situation, it should work. Feel free to improve on it.
It should also work for folder junctions if you use MOUNTPOINT instead of SYMBOLIC_LINK.
You may way to check that
if you put this into something you're releasing, since this form of symbolic link is only supported on Vista+.
问题是,正如此处所解释的,Windows自己对符号链接功能的支持不同的 Windows 版本有所不同,因此,例如,在 Vista 中(需要做很多工作),您可以获得比 XP 或 2000 更多的功能(据我所知,在其他 win32 版本上没有)。或者您也可以使用快捷方式,当然快捷方式有其自己的限制,并且并不“真正”等同于 Unix 符号链接。所以,你必须准确地指定你需要什么功能,你愿意在跨 win32 操作的祭坛上牺牲多少功能,等等——然后,我们可以制定出如何实现你所选择的妥协方案
ctypes
或win32all
调用...从某种意义上说,这是最少的。Problem is, as explained e.g. here, that Windows' own support for the functionality of symbolic links varies across Windows releases, so that e.g. in Vista (with lots of work) you can get more functionality than in XP or 2000 (nothing AFAIK on other win32 versions). Or you could have shortcuts instead, which of course have their own set of limitations and aren't "really" equivalent to Unix symbolic links. So, you have to specify exactly what functionalities you require, how much of those you are willing to sacrifice on the altar of cross-win32 operation, etc -- THEN, we can work out how to implement the compromise you've chosen in terms of
ctypes
orwin32all
calls... that's the least of it, in a sense.我将以下内容放入 Lib/site-packages/sitecustomize.py
I put the following into Lib/site-packages/sitecustomize.py
Juntalis 的代码不处理 Unicode,因此我将其修改为使用 ctypes,并使用 struct 对其进行了简化。我还查阅了 中的代码在 python ctypes 模块中使用结构体作为函数参数
Juntalis's code does not handle Unicode so I modified it to use ctypes and also simplified it using struct. I've also consulted the code from Using a struct as a function argument with the python ctypes module
正如另一个答案中提到的,使用 subprocess.call 可能是 Windows 的最佳选择。然而直接调用“mklink”可能会导致:
在 Windows Server 2016 上,我能够对文件执行以下操作:
按照 mklink 文档更改上面的开关。
As mentioned in another answer, using subprocess.call is likely the best option for windows. However calling 'mklink' directly may result in:
On Windows Server 2016, I was able to get the following to work for files:
Change the switches above as per mklink docs.
尝试在 Windows 中创建符号链接我总是收到错误
但是使用此代码创建快捷方式时我成功了
注意:确保快捷方式以“.lnk”结尾
Trying to create a Symlink in Windows I always got the error
However I was successfull when creating a shortcut with this code
Note: make sure the shortcut ends with '.lnk'
这是包含 kernel32.dll 所有方法的链接
http://www.geoffchappell .com/studies/windows/win32/kernel32/api/
我在 Windows xp sp3 上使用了 CreateHardLinkA,它有效!
导入ctypes
如果 os.path.exists(link_file):
os.remove(链接文件)
here is the link containing all methods of kernel32.dll
http://www.geoffchappell.com/studies/windows/win32/kernel32/api/
I used CreateHardLinkA on Windows xp sp3, it worked!
import ctypes
if os.path.exists(link_file):
os.remove(link_file)
python3 支持 windows vista+(windows 10...) 上的符号链接,只需在提升的 cmd 中运行
python3 supports symlinks on windows vista+(windows 10...) just need to run in an elevated cmd