python写的常用脚本,用到的时候快速修改

来源:互联网 发布:淘宝设置定时上架 编辑:程序博客网 时间:2024/06/05 10:22

[TOC]

参考

  1. Python实现递归遍历文件夹并删除文件

  2. Python实现递归遍历文件夹并删除文件 别人博客

  3. GBK编码的java文件批量转换为utf-8

    Eclipse workspace 工程java文件编码问题

解决问题:eclipse workspaces中的项目有的是GBK编码,如果导入到workspaces的默认编码为utf-8编码的话中文会出现乱码。
下面python3的方式递归变量当前目录以及子目录,把目录中的*.java文件由gbk转换为utf-8,注意只能用一次,一次之后当前目录以及子目录下的文件编码均会由gbk转为utf-8。

import codecsdef ReadFile(filePath,encoding="gbk"):    with codecs.open(filePath,"r",encoding) as f:        return f.read()def WriteFile(filePath,u,encoding="utf-8"):    with codecs.open(filePath,"w",encoding) as f:        f.write(u)def UTF8_2_GBK(src,dst):    content = ReadFile(src,encoding="gbk")    WriteFile(dst,content,encoding="utf-8")import osimport os.path# 递归遍历rootdir目录,把目录中的*.java编码由gbk转换为utf-8def ReadDirectoryFile(rootdir):    for parent,dirnames,filenames in os.walk(rootdir):            #case 1:            for dirname in dirnames:                    print("parent folder is:" + parent)                    print("dirname is:" + dirname)            #case 2            for filename in filenames:                        print("parent folder is:" + parent)                    print("filename with full path:"+ os.path.join(parent,filename))                    if filename.endswith(".java"):                            UTF8_2_GBK(os.path.join(parent,filename),os.path.join(parent,filename))                            print("Java文件")if __name__=="__main__":    ReadDirectoryFile(".")

eclipse 工程jsp文件编码以及头

<%@page contentType="text/html;charset=gbk"%>批量转换为<%@page contentType="text/html;charset=UTF-8"%>
大部分和上面编码类似,只是多了一个字符串的替换,重写了ReadFile方法,按行读取,然后替换字符"charset=gbk"-->"charset=UTF-8"

import codecsdef ReadFile(filePath,encoding="gbk"):    try:        strfile=""        f = codecs.open(filePath,"r",encoding)        line = f.readline()        while(line):            line=line.replace("charset=gbk","charset=UTF-8")#            line=line.replace("charset=GBK","charset=UTF-8")#            line=line.replace("charset=gb2312","charset=UTF-8")#            strfile+=line            line=f.readline()        f.close()        return strfile    except Exception:        return Nonedef WriteFile(filePath,u,encoding="utf-8"):    with codecs.open(filePath,"w",encoding) as f:        f.write(u)def UTF8_2_GBK(src,dst):    content = ReadFile(src,encoding="gbk")    #print(content)    WriteFile(dst,content,encoding="utf-8")import osimport os.path# 递归遍历rootdir目录,把目录中的*.java编码由gbk转换为utf-8def ReadDirectoryFile(rootdir):    for parent,dirnames,filenames in os.walk(rootdir):            #case 1:            for dirname in dirnames:                    print("parent folder is:" + parent)                    print("dirname is:" + dirname)            #case 2            for filename in filenames:                        print("parent folder is:" + parent)                    print("filename with full path:"+ os.path.join(parent,filename))                    if filename.endswith(".jsp"):                            UTF8_2_GBK(os.path.join(parent,filename),os.path.join(parent,filename))                            print("jsp文件")if __name__=="__main__":    ReadDirectoryFile(".")
原创粉丝点击