#介绍文件的操作方式

2018-08-12  本文已影响0人  派克_i

1.文件的打开操作

f = open("1.txt","w")

2.文件的关闭操作

f = open("1.txt","w")
f = close()

3.文件的读写

f = open("1.txt","w")
f.write("我爱中国")

4.读数据(read) 读单个长度

f = open("1.txt","r")
content = f.read(5)
print(content)
print("*"*30)
content = f.rand()
print(content)
f.close()

5.读数据(readlines) 读所有行

f = open("1.txt","r")
content = f.randlines()
print(type(content))
i = 1
for temp in content:
      print("%d:%s"%(i,temp))
      i =+ 1
f.close()

6.读数据(readline)读一行

f = open("1.txt","r")
content = f.readline()
print("1:%s"%content)
content = f.readline()
print("2:%s"%content)
f.close()
上一篇 下一篇

猜你喜欢

热点阅读