Files

Handling text files

1
2
3
4
5
6
7
8
9
f1 = open(“FileA.txt”, “r”)
f2 = open(“FIleB.txt”, “w”)
for i in f1:
if i == “”:
f2.write(“...”)
else:
f2.write(i)
f1.close()
f2.close()

Handling random files

1
2
3
4
5
6
7
8
9
10
import pickle
f1 = open(“FileA.dat”, “rb”)
f2 = open(“FileB.dat”, “wb”)
for i in f1:
if i == “”:
f2.write(“...”)
else:
f2.write(i)
f1.close()
f2.close()

File modes

... = open(“FileA.dat”, “rb”)

Mode Python
只读 "rb"
清空+写入 "wb"
创建+写入 "wb+"
续写 "ab"