Windows 上 Python 中的长路径 [英] Long paths in Python on Windows

查看:57
本文介绍了Windows 上 Python 中的长路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 Windows 下运行的 Python 编程时遇到问题.我需要处理超过 256 个或 whatsathelimit 字符的文件路径.现在,我基本上已经阅读了两种解决方案:

I have a problem when programming in Python running under Windows. I need to work with file paths, that are longer than 256 or whatsathelimit characters. Now, I've read basically about two solutions:

  1. 使用 kernel32.dll 中的 GetShortPathName 并以这种方式访问​​文件.

这很好,但我不能使用它,因为我需要以某种方式使用路径

That is nice, but I cannot use it, since I need to use the paths in a way

shutil.rmtree(short_path)

其中 short_path 是一个非常短的路径(类似于 D:\tools\Eclipse),而长路径出现在目录本身中(该死的 Eclipse 插件).

where the short_path is a really short path (something like D:\tools\Eclipse) and the long paths appear in the directory itself (damn Eclipse plugins).

  1. 在路径前添加"\\\\?\\"

我还没有设法以任何方式完成这项工作.尝试以这种方式执行任何操作总是会导致错误 WindowsError: [Error 123] The filename, directory name, or volume label syntax is wrong: <path here>

I haven't managed to make this work in any way. The attempt to do anything this way always result in error WindowsError: [Error 123] The filename, directory name, or volume label syntax is incorrect: <path here>

所以我的问题是:如何使第二个选项起作用?我强调我需要以与选项 #1 中的示例相同的方式使用它.

So my question is: How do I make the 2nd option work? I stress that I need to use it the same way as in the example in option #1.

还有什么办法吗?

我需要在 Python 2.7 中工作的解决方案

I need the solution to work in Python 2.7

问题 Windows 中的 Python 长文件名支持中断确实用魔法前缀"给出了答案,我在这个问题中表示我知道这一点.我不知道的是我如何使用它.我已经尝试将它添加到路径中,但它失败了,正如我上面写的那样.

The question Python long filename support broken in Windows does give the answer with the 'magic prefix' and I stated that I know it in this question. The thing I do not know is HOW do I use it. I've tried to prepend that to the path but it just failed, as I've written above.

推荐答案

好吧,似乎和往常一样,在我认真地向某人询问了二十分钟之后,我已经找到了困扰我一周的问题的答案.

Well it seems that, as always, I've found the answer to what's been bugging me for a week twenty minutes after I seriously ask somebody about it.

所以我发现我需要确保正确完成两件事:

So I've found that I need to make sure two things are done correctly:

  1. 路径只能包含反斜杠,不能包含正斜杠.
  2. 如果我想做一些类似列出目录的事情,我需要用反斜杠结束路径,否则Python会在它后面附加/*.*,这是一个正斜杠,也就是不好.
  1. The path can contain only backslashes, no forward slashes.
  2. If I want to do something like list a directory, I need to end the path with a backslash, otherwise Python will append /*.* to it, which is a forward slash, which is bad.

希望至少有人会觉得这很有用.

Hope at least someone will find this useful.

这篇关于Windows 上 Python 中的长路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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