'''w模式每次操作前会清空文件里的内容,然后写入写的内容'''
def write():
f = open('aaa.txt', 'w')
f.write('hello python\n')
f.close()
with open('aaa.txt', 'w') as f: # 不需要写f.close()这个操作的写法
f.write('this is write file\n')
write()
def read():
f = open('aaa.txt', 'r')
print(f.read())
f.close()
with open('aaa.txt', 'r') as f: # 不需要写f.close()这个操作的写法
f.read()
read()
def aWrite():
f = open('aaa.txt', 'a')
f.write('hello xia\n')
f.close()
with open('aaa.txt', 'a') as f: # 不需要写f.close()这个操作的写法
f.write('this is a write file')
aWrite()
读取文件的某个内容就涉及到文件的指针
def readPoint():
f = open('aaa.txt', 'r')
print(f.tell())
f.seek(6)
print(f.read())
f.close()
readPoint()