修改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)