Python文件读写 - 读一个文件所有行,加工后写另一个文件
1 #Filename: file_read_and_write.py 2 #打开文件,cNames读取所有行,储存在列表中,循环对每一行在起始处加上序号1,2,3,4 3 with open(r'file/companies.txt') as f1: 4 cNames = f1.readlines() 5 for i in range(0,len(cNames)): 6 cNames[i] = str(i+1) + '.' + '' + cNames[i] 7 8 #将处理过的cNames写入新的文件中 9 with open(r'file/scompanies.txt','w') as f2: 10 f2.writelines(cNames)
输入: companies.txt
google
tencent
alibaba
baidu
输出:scompanies.txt
1.google
2.tencent
3.alibaba
4.baidu
1.001^365=1.44......life is a project
原创文章,转载请注明出处!