s = open('1.txt','r').readlines() #读取成每一行构成的列表out=open('out.txt','w') #打开一个输出文件用以写入n=len(s)for i in range(n):out.write("%s %s"%(s[i],[i])) #向输出文件中写入:第i行、数字iout.close()
123456filename="文件名"result=[]for f in open(filename): result.append(f)result[-1]='c'+result[-1]open(filename,'w').writelines(result)将文件最后一行前加上字符 c