import chardet
import os
# ANSI文件转UTF-8
import codecs
import os def strJudgeCode(str):
return chardet.detect(str) def readFile(path): f = open(path, 'r',endoding='ANSI')
filecontent = f.read()
f.close() return filecontent def WriteFile(str, path):
try:
f = open(path, 'w')
f.write(str)
finally:
if f:
f.close() def converCode(path):
file_con = readFile(path)
result = strJudgeCode(file_con)
#print(file_con)
if result['encoding'] == 'utf-8':
#os.remove(path)
a_unicode = file_con.decode('utf-8')
gb2312 = a_unicode.encode('gbk')
WriteFile(gb2312, path) def listDirFile(dir):
list = os.listdir(dir)
for line in list:
print(line)
filepath = dir+line
print(filepath)
# if os.path.isdir(filepath):
# listDirFile(filepath)
# else:
# print(line)
converCode(filepath) if __name__ == '__main__': # listDirFile('./TRMD/') # 文件所在目录
file_path =r"C:\\Users\\Lenovo\\Desktop\\数据库设计\\爬虫脚本\\TRMD\\test"
files = os.listdir(file_path) for file in files:
file_name = file_path + '\\' + file
f = codecs.open(file_name, 'r','cp852')
ff = f.read()
file_object = codecs.open(file_path + '\\' + file, 'w', 'utf-8')
file_object.write(ff)

随机推荐

  1. C# 获取文件名、目录、后缀、无后缀文件名、扩展名、根目录等

    [csharp] view plain copy class Program { static void Main(string[] args) { //获取当前运行程序的目录 string file ...

  2. vue深入了解组件——Prop

    一.Prop的大小写(camelCase vs kebab-case) HTML中的特性名是大小写不敏感的,所以浏览器会把所有大写字符解释为小写字符.这意味着当你使用DOM中的模板时,cameCase ...

  3. ubuntu 安装搜狗输入法

    from:http://blog.csdn.net/qq_21792169/article/details/53152700 最近开始学习linux 在安装输入法中遇到的一些问题,最终成功安装,也得益 ...

  4. python post json applidation/json

    如果使用headers = {"Content-Type": "application/json"},在后台使用request.POST.get()无法获取数据 ...

  5. Nginx 错误汇总

    1. Upstream timed out (110: Connection timed out) while reading response header from upstream 这种情况主要 ...

  6. oozie错误:javax.servlet.jsp.el.ELException: variable [***] cannot be resolved

    完整错误: javax.servlet.jsp.el.ELException: variable [compute] cannot be resolved at org.apache.oozie.ut ...

  7. Frog and Portal(思维好题)

    Frog and Portal https://hihocoder.com/problemset/problem/1873 时间限制:1000ms 单点时限:1000ms 内存限制:512MB 描述 ...

  8. iOS codeview

    1.环境配置 oclint:http://oclint.org/ xcpretty:https://github.com/supermarin/xcpretty 使用Mac安装xcpretty过程可能 ...

  9. apache配置防盗链

    1.确保apache已开启rewrite.   2.在.htaccess文件中添加如下: RewriteEngine On RewriteCond %{HTTP_REFERER} !^http://X ...

  10. JSF控件的immediate属性和页面生命周期

    JSF中的控件基本都有immediate属性,对于这个属性的使用总结如下,更详细内容可参考Oracle官方文档. 1,为了更好的理解immediate属性,先看一下JSF页面的生命周期: JSF页面的 ...