dropbox中的文件编码GBK-->UTF

dropbox绝对是个好的cloud store tool,但是被qiang了,但这并部妨碍我使用这一款软件。在ubuntu上安装dropbox,需nautilus,可到google查找,在这里就不多说这些!

 

工作环境:ubuntu11.04

由于之前在win 7中使用dropbox保存了很多txt格式的文本文件,但在ubuntu上,却经常是无法中文乱码,搞的很郁闷!

 

闲下无事,就用最近在学的python写了个编码转换的列子,后续再做文件夹迭代,代码奉上:

 

#coding=utf-8

all = []

def readFile(filename):
    file = open(filename,'r')
    tmp = filename + '_tmp'
    fw = open(tmp,'w')
    for line in file:
        line = unicode(line,"gbk")
        all.append(line)
    file.close()

def writeFile(filename):
    #pass
    file = open(filename,'w')
    for line in all:
        line = line.encode('utf8')
        #print line
        file.write(line)

    file.close()
   

def main():
    filename='/home/gavin/scp.txt'
    tmp = filename + '_tmp'
    readFile(filename)
    writeFile(tmp)

if __name__=='__main__':
    main()

 

python学习中,请多指教!^_^

你可能感兴趣的:(Dropbox)