python读写log文件的例子。
代码:
#Filename: using_file.py
#site: www.jb200.com
#
poem = '''
Programming is fun
When the work is done
if you wanna make your work also fun:
use Python!
'''
e = open('../poem.txt','w')
e.write(poem)
e.close()
new = []
f = open('../poem.txt','r+')
n = 0
for i in f.readlines():
newline = "%d %s"%(n,i)
new.append(newline)
n += 1
f.close()
f = open("poem_new.txt","w+")
f.writelines(new)
f.close()
代码说明:
1,打开文件格式,为写,‘w’
2,每读出一句,然后就处理log,加上行首,重新写到新文件。