如何在Python中将'\\?\'附加到文件路径的前面 [英] How to append '\\?\' to the front of a file path in Python

查看:302
本文介绍了如何在Python中将'\\?\'附加到文件路径的前面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Python中使用一些长文件路径(Windows),并且遇到了一些问题.在此处中阅读问题之后,好像我需要附加为了在os.stat(filepath)中使用它们,在我的长文件路径的最前面添加了"\\?\".我遇到的问题是我无法在Python中创建以反斜杠结尾的字符串.问题此处指出您甚至无法在Python中使用单个'\'字符结束字符串.

I'm trying to work with some long file paths (Windows) in Python and have come across some problems. After reading the question here, it looks as though I need to append '\\?\' to the front of my long file paths in order to use them with os.stat(filepath). The problem I'm having is that I can't create a string in Python that ends in a backslash. The question here points out that you can't even end strings in Python with a single '\' character.

在任何Python标准库中或任何其他地方,是否有任何内容可以让您简单地将'\\?\'附加到您已经拥有的文件路径的前面?还是在Windows中使用Python处理长文件路径还有其他解决方法?看起来这很简单,但是我一生都无法解决.

Is there anything in any of the Python standard libraries or anywhere else that lets you simply append '\\?\' to the front of a file path you already have? Or is there any other work around for working with long file paths in Windows with Python? It seems like such a simple thing to do, but I can't figure it out for the life of me.

推荐答案

"\\\\?\\"应该为您提供所需的字符串.

"\\\\?\\" should give you exactly the string you want.

更长的答案:当然,您可以在Python中以反斜杠结束字符串.当它是原始"字符串(以'r'开头的字符串)时,您就不能这样做.通常用于包含(很多)反斜杠的字符串(以避免臭名昭著的倾斜牙签"综合征;-))

Longer answer: of course you can end a string in Python with a backslash. You just can't do so when it's a "raw" string (one prefixed with an 'r'). Which you usually use for strings that contains (lots of) backslashes (to avoid the infamous "leaning toothpick" syndrome ;-))

这篇关于如何在Python中将'\\?\'附加到文件路径的前面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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