给批量改变文件编码的程序做一个简单的图形界面

批量改变文件编码格式原文

import os
import chardet
from tkinter import *
from tkinter.filedialog import *

items = []
change = {}
app = Tk()
app.title("编码格式转换")
app.geometry("900x600+150+60")


def choose_file():
	global items 
	file_name = askopenfilename(initialdir="f:\\", filetypes=[("文本文件", "*.txt")])
	items.append(file_name)
	print("已选择文件>>> " + str(items))
	show_items(items)

def choose_dir():
	global items
	path = askdirectory()
	lst = os.listdir(path)
	for item in lst:
		new_path = path + os.sep + item
		if os.path.isfile(new_path) and new_path.endswith(".txt"):
			items.append(new_path)
	print("已选择文件夹>>> " + str(items))
	show_items(items)


def delete_list():
	global items
	items = []
	print("列表已清空>>> " + str(items))
	show_items(items)


def chage_file(mat="utf-8"):
	global items
	global change
	for file in items:
		if file:
			fo = open(file, "rb")
			encod = chardet.detect(fo.read())['encoding']
			fo.close()
			# print(encod)
			with open(file, "r", encoding=encod) as f:
				content = f.readlines()
				content = "".join(content)
			with open(file, "w", encoding=mat) as w:
				w.write(content)
			change[file] = encod
			show_change(change)


def show_items(items):
	items = "\n".join(items)
	label = app.children["content"]
	label['text'] = str(items)

def show_change(change):
	total = []
	for key in change.keys():
		text  = key+" 成功由 "+change[key]+" 转换为 utf-8 格式。"
		total.append(text)
	label = app.children["content"]
	total = "\n".join(total)
	label['text'] = total


Label(name="content", text=">>>已选文件汇总<<<", font=('楷体',15)).pack(fill=BOTH,expand=1)

Button(name='choose_file', text='选择文件', bg='pink', font=('楷体', 25), command=choose_file).pack(fill=X,expand=1,padx=10,side=LEFT)
Button(name='choose_dir', text='选择文件夹', bg='pink', font=('楷体', 25), command=choose_dir).pack(fill=X,expand=1,padx=10,side=LEFT)
Button(name='delete_list', text='清空已选文件', bg='pink', font=('楷体', 25), command=delete_list).pack(fill=X,expand=1,padx=10,side=LEFT)

Label(name="zero", text="↓ ↓ 选择转换编码格式 ↓ ↓", font=(15)).place(x=620,y=500)
# .pack(fill=BOTH,expand=True)
Button(name="code1", text="utf-8", bg='orange', command=lambda:chage_file("utf-8")).pack(fill=BOTH,expand=1,side=RIGHT)
Button(name="code2", text="gbk", bg='orange', command=lambda:chage_file("gbk")).pack(fill=BOTH,expand=1,side=RIGHT)
Button(name="code3", text="ANSI", bg='orange', command=lambda:chage_file("ANSI")).pack(fill=BOTH,expand=1,side=RIGHT)
Button(name="code4", text="Unicode", bg='orange', command=lambda:chage_file("unicode")).pack(fill=BOTH,expand=1,side=RIGHT)

app.mainloop()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hao难懂

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值