#file操作要点: Aaa.txt for Desktops
With open (R ' C:\Users\xuyin\Desktop\aaa.txt ', ' R ') as F:
X1=f.read () #得到一个字符串
Print type (x1)
Print (x1)
F.seek (0,0)
Print (1)
X2=f.readline () #得到第一行, forming a string
Print type (x2)
Print (x2)
# F.seek (0,0), no this line reads all the remaining rows
Print (F.readlines ()) #得到一个列表, each line as an element
With open (R ' C:\Users\xuyin\Desktop\aaa.txt ', ' w+ ') as F:
F.write (' What does You Like?\nboth ') #清空原来的重新写
F.writelines (' 123\n ') #接着写,
F.writelines (' Thank you ')
F.seek (0,0)
Print (1)
Print F.read ()
Ans
<type ' str ' >
What does it like?
Both123
Thank
1
<type ' str ' >
What does it like?
[' both123\n ', ' thank you ']
1
What does it like?
Both123
Thank
Remark: No F.wirteline
#从末尾开始写:
With open (R ' C:\Users\xuyin\Desktop\aaa.txt ', ' A + ') as F:
F.write (' See you! ')
F.seek (0,0)
Print (F.read ())
Ans
What does it like?
Both123
Thank YouSee You!
File read type:??
W: Write only
R:
w+
r+
A +: Append
After encountering binary, add ' B ': Rb,wb ...
Python (2): File