python中的中文路径解决

python中的中文路径解决:

注:
1、sys.setdefaultencoding('utf-8')将python默认encode改为utf-8
2、p.write(s.encode('utf-8')+"\n")写入时再encode('utf-8')

vi a1.py

#!/usr/bin/python
#coding=utf-8

import os
import shutil
import sys

reload(sys)
sys.setdefaultencoding('utf-8')

dir="/root/python"

for root,dirs,files in os.walk(dir):
for f in files:
if f == "bb.txt":
s = os.path.dirname(os.path.join(root,f))
p = open('/root/python/logs/py.log','a')
p.write(s.encode('utf-8')+"\n")
p.close()

c = open('/root/python/logs/py.log','r')
for i in c.readlines():
shutil.rmtree(i.strip())

:wq

python a1.py

猜你喜欢

转载自blog.51cto.com/yangzhiming/2128499