马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 风不会停息 于 2018-7-20 16:01 编辑
1. 导入: import easygui as g, easygui模块所有操作方法: http://bbs.fishc.com/thread-46069-1-1.html
gui(Graphical User Interface 图形用户界面)
附两个 035动动手3, 4 程序:
import easygui as g
import os
file = g.fileopenbox(title = '选择文件', default = "D:\", filetypes = ['*.txt'])
msg = ('文件【%s】的内容如下:' % os.path.split(file)[1])
title = '显示文件内容'
with open(file) as f:
file_content = f.read()
reply = g.textbox(msg, title, file_content)
if reply != file_content + '\n': #reply 会多一空行
choice = g.buttonbox('检测到内容发生改变, 请选择以下操作:', '警告', ('覆盖保存', '放弃保存', '另存为...'))
if choice == '覆盖保存':
with open(file, 'w') as f:
f.write(reply)
with open(file) as f:
if f.read() == reply:
g.msgbox('保存成功', ok_button = '结束')
elif choice == '放弃保存':
g.msgbox('内容未保存', ok_button = '结束')
elif choice == '另存为...':
save_path = g.filesavebox(title = '选择目录', default = '1_%s' % os.path.split(file)[1])
with open(save_path, 'w') as f:
f.write(reply)
with open(save_path) as f:
if f.read() == reply:
g.msgbox('保存成功', ok_button = '结束')
import easygui as g
import os
code_extension = ('.asm', '.py', '.c', '.cpp', '.html')
code = dict()
code_lines = code.fromkeys(code_extension, 0)
code_files = code_lines.copy()
def count_code_lines(path):
count = 0
with open(path, encoding = 'UTF-8') as f:
print('正在分析文件 %s ...' % path)
try:
for each_line in f:
count += 1
except UnicodeDecodeError: #忽略掉格式不兼容的文件
pass
extension = os.path.splitext(path)[1]
code_lines[extension] += count
def count_code_files(path):
extension = os.path.splitext(path)[1]
code_files[extension] += 1
def search_files(path):
try:
os.chdir(path)
for each_file in os.listdir(os.curdir):
if os.path.isdir(each_file):
search_files(each_file)
os.chdir(os.pardir)
if os.path.isfile(each_file):
if os.path.splitext(each_file)[1] in code_extension:
count_code_lines(os.getcwd() + os.sep + each_file)
count_code_files(os.getcwd() + os.sep + each_file) #递归搜寻文件
except PermissionError:
pass
def show_result():
count = 0
content = ''
for each_key in code_files.keys():
content += ('【%s】源文件 %d 个, 源代码 %d 行\n' % ( each_key, code_files[each_key], code_lines[each_key] ) )
for each_value in code_lines.values():
count += each_value
msg = ("您目前共累积编写了 %d 行代码, 完成进度: %.3f %%\n离 10 万行代码还差 %d 行, 请继续努力!" % ( count, count * 100/100000, 100000 - count) )
g.textbox(msg = msg, title = '统计代码', text = content)
g.msgbox('请打开您所有存放代码的文件夹', '统计代码量....')
path = g.diropenbox('请选择您的代码库', '浏览文件夹', default = 'D:\\')
search_files(path)
show_result()
|