python open函数的中文路径报错

如下:

# 打开一个文件
# fo = open("D://temp//t1.txt", "r") #T
# fo = open("D:/temp/t1.txt", "w") #T
# fo = open("D:/temp/t1.txt", "r") #T
# fo = open("D:/temp/t1.txt", "w") #T
# fo = open("D:/测试文件/t1.txt", "r") #F,IOError: [Errno 2] No such file or directory: 'D:/\xe6\xb5\x8b\xe8\xaf\x95\xe6\x96\x87\xe4\xbb\xb6/t1.txt'
# fo = open("D://测试文件//t1.txt", "r") #F,IOError: [Errno 2] No such file or directory: 'D:/\xe6\xb5\x8b\xe8\xaf\x95\xe6\x96\x87\xe4\xbb\xb6/t1.txt'
# fo = open("D://测试文件//t1.txt", "w") #F,IOError: [Errno 2] No such file or directory: 'D:/\xe6\xb5\x8b\xe8\xaf\x95\xe6\x96\x87\xe4\xbb\xb6/t1.txt'
# fo = open("D:/测试文件/t1.txt", "w") #F,IOError: [Errno 2] No such file or directory: 'D:/\xe6\xb5\x8b\xe8\xaf\x95\xe6\x96\x87\xe4\xbb\xb6/t1.txt'
# fo = open(unicode("D:/测试文件/t1.txt","utf8"), "r") #T
# fo = open(unicode("D:/测试文件/t1.txt","utf-8"), "r") #T
# fo = open(unicode("D:/测试文件/t1.txt","UTF-8"), "r") #T
fo = open(unicode("D:/测试文件/t1.txt","UTF8"), "r") #T  -- 中文路径写成\  也正确
print fo.read();

print "文件名: ", fo.name
print "是否已关闭 : ", fo.closed
print "访问模式 : ", fo.mode
print "末尾是否强制加空格 : ", fo.softspace
# t1.txt的内容
# 文件名:  D:/测试文件/t1.txt
# 是否已关闭 :  False
# 访问模式 :  r
# 末尾是否强制加空格 :  0

END

 

 

 

 

 

你可能感兴趣的:(Python)