保存文件的方法
fns = 'test_out.txt'
with open(fns, 'w+') as fs:
print('hello world by python', file=fs)
将上节课的分析的分数结果保存至文件
import tkinter
import tkinter.filedialog
fn = tkinter.filedialog.askopenfilename(filetypes=[('TXT','.txt')])
with open(fn, 'r') as f:
lines = f.readlines()
print(lines)
fns = fn.replace('.txt', '_out.txt')
print(fns)
with open(fns, 'w+') as fs:
print('name', 'sum', 'avg', file=fs)
for l in lines:
if l.startswith('name'):
continue
cols = l.split()
name = cols[0]
sum = float(cols[1])+float(cols[2])+float(cols[3])
print(name, sum, sum/3, file=fs)