源代码如下:
# .exe .txt .ppt .avi .jpg .mp4
f = open('D:\\SuShi.txt') # 默认为 r
print(f.read())
print(f.tell()) # 文件长度
f.seek(9, 0) # 指针定位(从7开始,到末尾)
print(f.readline()) # 整行输出
print('-----------')
f.seek(0, 0)
for each_line in f:
print(each_line)
f.close()
print('-----------------------------')
# 编写文件
f = open('D:\\test.txt', 'w')
f.write('我爱静静')
f.close()
f = open('D:\\test.txt')
print(f.read())
运行结果如下:
A:I love JingJing.
B:Romeo, take me somewhere we can be alone.
----------------------------------------------
A:I'll be waiting; all there's left to do is run.
B:You'll be the prince and I'll be the princess
----------------------------------------------
A:It's a love story baby just say :"Yes"
301
JingJing.-----------
A:I love JingJing.B:Romeo, take me somewhere we can be alone.
----------------------------------------------
A:I'll be waiting; all there's left to do is run.
B:You'll be the prince and I'll be the princess
----------------------------------------------
A:It's a love story baby just say :"Yes"
-----------------------------
我爱静静