简体   繁体   English

Windows 10 Creators 更新中的 python 符号链接

[英]python symlink in windows 10 creators update

Since the windows 10 creator update, you can enable developer mode to circumvent administrator privileges when creating a symlink.自 Windows 10 Creator 更新以来,您可以在创建符号链接时启用开发人员模式以规避管理员权限。 Now, I was able to create a symlink using mklink like this:现在,我可以使用mklink创建一个符号链接,如下所示:

os.system('mklink %s %s' %(dst, src))

Hopefully it's obvious that dst is the destination symlink path, and src is the source file for the symlink.希望很明显dst是目标符号链接路径,而src是符号链接的源文件。 While it seems to work ok, it doesn't error if it fails which makes it a little more difficult to ensure each symlink is successful.虽然它似乎工作正常,但如果失败也不会出错,这使得确保每个符号链接成功变得更加困难。 I can check if the path exists after each symlink, but that's less efficient than a try/except clause.我可以检查每个符号链接后是否存在路径,但这比 try/except 子句效率低。 There's also what looks like a command shell window(?) that pops up and closes quickly every time - and that's really annoying when you're symlinking a lot of files...还有一个看起来像一个命令外壳窗口(?)的东西,每次都会快速弹出和关闭——当你对很多文件进行符号链接时,这真的很烦人......

So, I've been trying other options I've found on stack overflow like this one: How to create symlinks in windows using Python?因此,我一直在尝试在堆栈溢出中找到的其他选项,例如: How to create symlinks in windows using Python? Unfortunately, the CreateSymbolicLinkW command doesn't seem to work for me... I also found this: OS.symlink support in windows where it appears you need to adjust the group policy editor;不幸的是, CreateSymbolicLinkW命令似乎对我不起作用......我还发现了这一点: OS.symlink support in windows似乎需要调整组策略编辑器; however, it apparently still requires users in the administrator group to run the process as an administrator even if you explicitly set that user with symlink privileges.但是,它显然仍然要求管理员组中的用户以管理员身份运行该进程,即使您明确设置该用户具有符号链接权限。

With the windows 10 creator update, there's mention of a new dwflag in the CreateSymbolicLink api ( SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE ) you can see the reference for that here: symlinks windows 10与Windows 10的创造者更新,有一个在CreateSymbolicLink API(SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE)的新dwflag提及您可以看到,这里的参考: 符号链接窗口10

Using the ctypes stuff is a bit over my head, so I'm wondering if anyone knows: Can I actually use that new dwflag?使用 ctypes 的东西有点超出我的头脑,所以我想知道是否有人知道:我真的可以使用那个新的 dwflag 吗? How do I use it?我如何使用它? Will it work without running the process as administrator?如果不以管理员身份运行进程,它会工作吗?

I use Autodesk Maya, so I'm stuck with python 2.7 options... I have not tried launching Maya as an administrator so I don't know if that will work, but it seems like a rather annoying hoop to jump through even if it does... I appreciate any assistance you can give我使用 Autodesk Maya,所以我坚持使用 python 2.7 选项......我没有尝试以管理员身份启动 Maya,所以我不知道这是否可行,但即使跳过它似乎也很烦人它确实......我很感激你能提供的任何帮助

it doesn't error if it fails如果失败,它不会出错

os.system will return the exit status of the call. os.system 将返回调用的退出状态。 It does not raise an exception.它不会引发异常。

If you look at the docs for os.system, they recommend using the subprocess module.如果您查看 os.system 的文档,他们建议使用 subprocess 模块。 In fact, subprocess.check_call does what you describe (raise an exception on a non-zero exit status).事实上, subprocess.check_call做你所描述的(在非零退出状态上引发异常)。 Perhaps that would work better.或许那样效果会更好。

On the other hand, the command mklink will return a zero exit status even if the source does not exist (it will create a link to non-existent file and return 0).另一方面,即使源不存在,命令 mklink 也将返回零退出状态(它将创建一个指向不存在文件的链接并返回 0)。 You might want to validate the actual link as you mentioned, depending on what errors you are trying to find.您可能想要验证您提到的实际链接,具体取决于您尝试查找的错误。

As far as hiding the console window, see this .至于隐藏控制台窗口,请参阅

os.symlink works out of the box since python 3.8 on windows, as long as Developer Mode is turned on. os.symlink开箱即用,因为 Windows 上的 python 3.8,只要开发人员模式打开。

Not sure whether this will help with Maya;不确定这对 Maya 是否有帮助; they seem to have committed to Python 3 though.不过,他们似乎已经致力于 Python 3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM