一定要记得调用close,来关闭文件。 -----箴言-----
1. 内容简介:
和其它语言一样,python也支持文件操作。
本例子展示了文件的基本操作:
打开,关闭,读写,更名,文件内容定位。
2.代码:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import os
#open close
def test_open():
#open
f = open ("hello.txt",'r')
print('file name:' + f.name)
print('file mode:' + f.mode)
is_closed = f.closed
print('file closed:') #文件是否已经处于关闭状态
print(is_closed)
print('\n\n')
#close
f.close()
#read write
def test_write():
#open
f = open ("hello.txt",'a+')
f.write('hello python!!!\n')
f.write('hello 2020!!!')
#seek,改变当前文件的位置到文件开头
f.seek(0,0)
read_str=f.read(100)
print('read_str:')
print(read_str)
f.close()
#main
if __name__ == '__mai