python 文件读写,删除指定行
# -*- coding:utf-8 -*-
import re
import os
import random
d1 = {}
""" 写文件"""
files = open('test.txt','a+')
strs = "世界很大"
for var in range(0,10):
for conu in range(0,10):
files.write(strs+str(var)+str(conu)+',')
files.write('\n')
files.close()
""" 读取文件"""
files = open('test.txt','r')
l2 = files.readlines()
print l2
for l2_concent in l2:
l3 =l2_concent.split(',')
print l3
l3.pop()
print l3
d1[l3[0]] = l3[1:]
print '\n\n'
print d1
files.close()
""" 删除文件指定行"""
files = open('test.txt','r+')
l4 = files.readlines()
l4[4]= ''
files.close()
files = open('test.txt','w+')
l4 = files.writelines(l4)
files.close()
---------------------
作者:qq_18220489
来源:CSDN
原文:https://blog.csdn.net/qq_18220489/article/details/78570809
版权声明:本文为博主原创文章,转载请附上博文链接!