在Python中创建NTFS连接点
有没有办法在Python中创建NTFS连接点? 我知道我可以调用 junction
实用程序,但最好不要依赖外部工具。
Is there a way to create an NTFS junction point in Python? I know I can call the junction
utility, but it would be better not to rely on external tools.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
根据 Charles 接受的答案,这里改进了(和跨平台)函数版本(Python 2.7 和 3.5+)。
Based on the accepted answer by Charles, here improved (and cross-platform) versions of the functions (Python 2.7 and 3.5+).
您不想依赖外部工具但又不介意依赖特定环境? 我认为您可以放心地假设,如果您运行的是 NTFS,那么连接实用程序可能就在那里。
但是,如果您的意思是您不想调用外部程序,我找到了 ctypes 的东西是无价的。 它允许您直接从 Python 调用 Windows DLL。 我很确定现在的标准 Python 版本中都有它。
您只需找出
CreateJunction()
(或任何 Windows 对其的调用)API 调用位于哪个 Windows DLL 中,并设置参数和调用。 祝你好运,微软似乎不太支持它。 您可以反汇编SysInternalsjunction
程序或linkd
或其他工具之一以了解它们是如何做到这一点的。我,我很懒,我只是将
junction
称为外部进程:-)You don't want to rely on external tools but you don't mind relying on the specific environment? I think you could safely assume that, if it's NTFS you're running on, the junction utility will probably be there.
But, if you mean you'd rather not call out to an external program, I've found the ctypes stuff to be invaluable. It allows you to call Windows DLLs directly from Python. And I'm pretty sure it's in the standard Python releases nowadays.
You'd just have to figure out which Windows DLL the
CreateJunction()
(or whatever Windows calls it) API call is in and set up the parameters and call. Best of luck with that, Microsoft don't seem to support it very well. You could disassemble the SysInternalsjunction
program orlinkd
or one of the other tools to find out how they do it.Me, I'm pretty lazy, I'd just call
junction
as an external process :-)我在类似问题中回答了这个问题,所以我将把我的答案复制到下面。 自从写完这个答案后,我最终编写了一个仅使用 python 的模块(如果您可以调用使用 ctypes python-only 的模块)模块来创建、读取和检查连接,这些连接可以在 此文件夹。 希望有帮助。
此外,与使用 CreateSymbolicLinkA API 的答案不同,链接的实现应该适用于支持联结的任何 Windows 版本。 CreateSymbolicLinkA 仅在 Vista+ 中受支持。
答案:
python ntfslink扩展
或者如果你想使用pywin32,您可以使用前面提到的方法,并读取,使用:
根据您的需要调整 CreateFile 中的属性,但对于正常情况,它应该可以工作。 请随意改进它。
如果您使用 MOUNTPOINT 而不是 SYMBOLIC_LINK,它也应该适用于文件夹连接。
您可以检查一下是否
将其放入要发布的内容中,因为这种形式的符号链接仅在 Vista+ 上受支持。
I answered this in a similar question, so I'll copy my answer to that below. Since writing that answer, I ended up writing a python-only (if you can call a module that uses ctypes python-only) module to creating, reading, and checking junctions which can be found in this folder. Hope that helps.
Also, unlike the answer that utilizes uses the CreateSymbolicLinkA API, the linked implementation should work on any Windows version that supports junctions. CreateSymbolicLinkA is only supported in Vista+.
Answer:
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+.
您可以使用 python win32 API 模块,例如
参见 http://docs.activestate.com /activepython/2.5/pywin32/win32file__CreateSymbolicLink_meth.html 了解更多详细信息,
如果您也不想依赖它,您可以随时使用 ctypes 并直接调用 CreateSymbolicLinl win32 API,无论如何,这是一个简单的调用,
这里是示例调用使用 ctypes
MSDN 说最低支持的客户端 Windows维斯塔
you can use python win32 API modules e.g.
see http://docs.activestate.com/activepython/2.5/pywin32/win32file__CreateSymbolicLink_meth.html for more details
if you do not want to rely on that too, you can always use ctypes and directly call CreateSymbolicLinl win32 API, which is anyway a simple call
here is example call using ctypes
MSDN says Minimum supported client Windows Vista
从 Python 3.5 开始,
_winapi
模块中有一个函数CreateJunction
。Since Python 3.5 there's a function
CreateJunction
in_winapi
module.