文件操作演示
"""
文件的读操作演示
"""
file = open("text",'w', buffering=-1)
fd = open("text",'r')
while True:
date = fd.read(2)
if not date:
break
print("读取到的内容:",date)
date = fd.readline(4)
print("读取到的内容:",date)
date = fd.readline()
print("读取到的内容:",date)
data = fd.readlines()
print("读取到的内容是:",data)
for line in fd:
print("每行内容:",line)
fd = open('text','rb')
data = fd.read()
print(data)
print(data.decode())
fd = open('text','wb')
fd.write(bytes('你好哇\nhello world',encoding = 'UTF-8'))
fd.write(bytes(b'hello world'))
fd.close()
"""
with 语句
"""
with open('text') as f:
data = f.read()
print(data)
"""
缓冲区示例 buffer
"""
fd = open('text','w')
while True:
s = input('>>')
fd.write(s)
fd.flush()
fd = open('text','a+')
print("当前文件偏移量位置",fd.tell())
fd = open('text','r+')
print(fd.read(2))
print("当前文件偏移量位置",fd.tell())
fd.seek(5,0)
print(fd.read(2))
fd.close()
"""
文件管理函数
"""
import os
a = os.path.getsize('text')
print(a)
a = os.listdir('/home/tarena/month02/day05')
print(a)
print(os.path.exists('text'))
print("-------------------------------------------")
print(os.path.isfile('text'))
os.remove('text')