python文件读写操作
读写小文件
eg
with open('1.txt','r') as f:
with open('2.txt',"w") as f1:
b=f.readlines()
print('--------')
print(b)
print('++++++++++++++++++')
for x in b:
f1.write(x)
print(x)
读写大文件
方法一重载,可以做文件传输
with open('1.txt','rb') as f:
while True:
block=f.read(1024)
if not block:
break
方法2每次读取一行
with open('1.txt','rb') as f:
while True:
block=f.readline()
if not block:
break
方法3最强大方法
with open('1.txt','r') as f:
while True:
for line in f:
# do something to line
break

浙公网安备 33010602011771号