python 读取中文路径

要读取文件名,当文件名含有中文名时如何处理?

将中文文件名写进txt没问题:


path = 'C:/Users/liesmars/Desktop/视频/'
with open('try.txt','a') as f:
	f.write(path)
f.close()

但是当要获取该文件夹下的文件就会出错,含有中文找不到该路径。

(1)用listdir

# -*- coding: utf-8 -*-
import os

uipath = unicode(path,'utf-8')
filelist = os.listdir(uipath)

for files in filelist:
	print files.encode('utf-8')



(2)os.walk


path1 = 'C:/Users/liesmars/Desktop/视频/'
uipath1 = unicode(path1,'utf-8')
for root,dirs,files, in os.walk(uipath1):
	for filename in files:
		filename = filename.encode('utf-8')
		root = root.encode('utf-8')
		filepath = os.path.join(root,filename)
		print 'filepath:',filepath

结果:

filepath: C:/Users/liesmars/Desktop/视频/无人机视频2.mp4
filepath: C:/Users/liesmars/Desktop/视频/视屏2\无人机拍摄视频.mp4

编码的问题还要再仔细看看。

unicode将中文转为计算机可以理解的语言

encode将unicode编码转为中文




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值