关于python处理文件路径的问题

2025年03月19日 02:16
有2个网友回答
网友(1):

中文路径无法使用可能是文字编码不同,在代码开头加上

#-*coding:936*-

在路径中有转义字符,可以用/代替\,如
C:/Users/asus//Desktop/cura
或在字符串前加r
r"C:\Users\asus\Desktop\cura"

网友(2):

tim@crunchbangtime:~/Documents$ ls *.txt
tasks.txt  中文文件.txt
tim@crunchbangtime:~/Documents$ python
Python 2.7.3 (default, Mar 14 2014, 11:57:14) 
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os.path
>>> os.path.exists(r'/home/tim/Documents/中文文件.txt')
True
>>>