read_write_test

简介

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# coding:utf-8

import io
f = open(r'./somefile.txt')
print(f.read(7))
print(f.read(4))
f.close()

f = open('./somefile.txt')
print(f.read())
f.close()

f = open('somefile.txt')
for i in range(3):
print(str(i) + ':'+f.readline(),end='')
f.close()

import pprint
pprint.pprint(open('somefile.txt').readlines())


f = open('somefile.txt','w')
f.write('this\nis no\nhaiku')
f.close()

f = open('somefile.txt')
lines = f.readlines()
print(lines)
f.close()
lines[1] ="isn't a\n" # 这里利用了 lines是可变对象的特性。
f = open('somefile.txt','w')
f.writelines(lines)
f.close()