f = open('data.txt', 'w+')
for i in range(100000):
f.write(str(random.randint(1,100)) + '\n')
print(f.read())
f.close()
2)
from collections import Counter
from collections import Iterable
dict={}
f = open('data.txt', 'r+')
print(isinstance(f, Iterable))
for i in f:
if i not in dict:
dict = 1
else:
dict = dict + 1
d = Counter(dict)
with open('mostNum.txt', 'w+') as k:
for i in d.most_common(10):
k.write(i[0].strip())
k.seek(0, 0)
print(k.read())
f.close()
注:
with open('/tmp/passwd', 'r+') as f1, open('/tmp/passwdNum', 'w+') as f2:
for i, j in enumerate(f1):
f2.write(j.strip()+str(i)+'\n')
f2.seek(0, 0)
print(f2.read())
注:
f = open('/tmp/passwd')
"""
f.buffer
<_io.BufferedReader name='/etc/passwd'>
bufferObj = f.buffer
from collections import Iterable
isinstance(bufferObj, Iterable)
True
for i in bufferObj:
print(i)
f.close()
# 当只是读取文件时, buffer里面没有内容
f = open("/tmp/passwd", 'a+')
f.read()
''
f.write('123')
3
# 此时写入的内容在缓冲区里面,并没有真正写入磁盘文件
f.buffer
<_io.BufferedRandom name='/tmp/passwd'>
for i in f.buffer:
print(i, end=',')