f.a*_*uri 7 python string error-handling filepath
作为我的程序的一部分,我需要在Python中以字符串的形式放置大量文件路径.例如,我的一个目录是D:\ful_automate\dl.但是Python将一些字符识别为其他字符并引发错误.在示例中,错误是IOError: [Errno 22] invalid mode ('wb') or filename: 'D:\x0cul_automate\\dl.它对我来说发生了很多,每次我需要将目录名称更改为可能没有问题的目录名称.
Mar*_*ers 16
的\字符被用于形成字符通道; \f有特殊意义.
改为使用/或使用原始字符串r''.或者,您可以通过使用额外的转义来确保Python将反斜杠作为反斜杠读取\.
r'D:\ful_automate\dl'
'D:\\ful_automate\\dl'
'D:/ful_automate/dl'
Run Code Online (Sandbox Code Playgroud)
演示以显示差异:
>>> 'D:\ful_automate\dl'
'D:\x0cul_automate\\dl'
>>> r'D:\ful_automate\dl'
'D:\\ful_automate\\dl'
Run Code Online (Sandbox Code Playgroud)