python easygui模块

修改txt文件内容

import easygui as g
import os
file_name =  g.fileopenbox()
print(file_name)
f1 = open(file_name,'r')
f2 = f1.read()
f1.seek(0,0)
msg1 = ('文件【%s】的内容如下' % os.path.basename(file_name))
f3 = g.textbox(msg = msg1,title = '显示文件内容',text = f1)
if f3[:-1] != f2:
    f4 = g.buttonbox('内容发生了改变,请选择以下操作',choices = (
        '覆盖保存','放弃保存','另存为……'))
    if f4 == '覆盖保存':
        with open(file_name,'w') as f5:
            f5.write(f3)
    if f4 == '另存为……':
        save_path = g.filesavebox()
        with open(save_path,'w') as f5:
            f5.write(f3)

在这里插入图片描述

在这里插入图片描述

统计写了多少行代码

file_path = r'D:\python\实例'
import os
import easygui as g
file = ['py','c','cpp']
file1 = ['py1','c1','cpp1']
count = {}
count1 = 0
text1 = []
def seek(path):
    list1 = os.listdir(path)
    for each in list1:
        if os.path.isfile(os.path.join(path,each)):
            (name,suffix) = each.split('.',1)
            if suffix in file:
                if suffix in count:
                    count[suffix] += 1
                else:
                    count[suffix] = 0
                    count[suffix+'1'] = 0
                with open(os.path.join(path,each),encoding = 'utf-8') as f1:
                    for each_line in f1:
                        count[suffix+'1'] += 1
        if os.path.isdir(os.path.join(path,each)):
            seek(os.path.join(path,each))
seek(file_path)
for each in count:
    if each in file1:
        count1 += count[each]
msg1 = ('您目前共累计编写了%d行代码,完成进度:%.2f%%\n离十万行代码还差%d行,请继续努力'
        % (count1,count1/100000,100000-count1))
for each in count:
    if each in file:
        text1.append('【.%s】源文件%d个,源代码%d行' % (each,count[each],count[each+'1']))
g.textbox(msg = msg1,text = text1)

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值