当前仓库属于关闭状态,部分功能使用受限,详情请查阅 仓库状态说明
1 Star 0 Fork 9

lineCode / Multi-environ Manager
关闭

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
pycodec.py 2.87 KB
一键复制 编辑 原始数据 按行查看 历史
tianduanrui 提交于 2018-01-27 01:24 . 更新转码器
import os
import sys
import codecs
import chardet
#detect codec
#read decode
#encode write
#maybe remove temp file
#file
#iconv
#charset
#codecs
#chardet
def ReadFile(filePath, encoding="utf-8"):
with codecs.open(filePath, "rb", encoding) as f:
return f.read()
def WriteFile(filePath, u, encoding="utf-8"):
with codecs.open(filePath,"wb") as f:
f.write(u.encode(encoding, errors="ignore"))
def utf(path, recursive=False):
#print(path, os.path.isfile(path))
if os.path.isfile(path) is True:
content = ""
with open(path, 'rb') as f:
content = f.read()
encodeName = chardet.detect(content)["encoding"]
content = ""
content = ReadFile(path, encodeName)
b1 = bytes(content, encoding=encodeName)
newContent = codecs.decode(b1, encodeName)
newEncodeName = "utf-8"
WriteFile(path, newContent, newEncodeName)
print(path, encodeName, newEncodeName)
else:
for i in os.listdir(path):
now_path = os.path.join(path, i)
#print(path, now_path, os.path.splitext(i)[1], os.path.isdir(now_path) )
if os.path.isdir(now_path) and recursive is True:
utf(now_path, recursive)
elif os.path.splitext(i)[1] == ".cpp" \
or os.path.splitext(i)[1] == ".cc"\
or os.path.splitext(i)[1] == ".hh"\
or os.path.splitext(i)[1] == ".sh"\
or os.path.splitext(i)[1] == ".c"\
or os.path.splitext(i)[1] == ".ui"\
or os.path.splitext(i)[1] == ".qrc"\
or os.path.splitext(i)[1] == ".rc"\
or os.path.splitext(i)[1] == ".in"\
or os.path.splitext(i)[1] == ".h"\
or os.path.splitext(i)[1] == ".pro"\
or os.path.splitext(i)[1] == ".pri"\
or os.path.splitext(i)[1] == ".c"\
or os.path.splitext(i)[1] == ".ui":
#print ("inside", now_path)
utf(now_path)
else:
continue;
#print ("excide", now_path)
usage = """
python pycodec.py haha.cpp #更改单文件
python pycodec.py haha #更改文件夹下的全部文本文件(.cpp .h)
python pycodec.py haha recursive #递归更改文件夹下的全部文本文件
"""
if __name__ == '__main__':
# sys.argv = ['main', r'C:\Users\weidiao\Desktop\电子书', 'recursive']
if len(sys.argv) == 1:
print(usage)
exit()
if len(sys.argv) > 3:
print(usage)
print('too many argument')
exit()
path = sys.argv[1]
if not os.path.exists(sys.argv[1]):
print(usage)
print('no this file or folder')
exit()
recursive = (len(sys.argv) == 3 and sys.argv[2] == 'recursive')
utf(path, recursive)
Python
1
https://gitee.com/lineco/PyMake.git
git@gitee.com:lineco/PyMake.git
lineco
PyMake
Multi-environ Manager
master

搜索帮助