这里只是学习非常简单的文件读写,只是做一个简单的了解。
1. 内建方法open的定义:open
(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)
2. Open file and return a corresponding file object. If the file cannot be opened, an OSError
is raised.
3. mode of 'r' : open for reading (default)
fr = open('example.txt', 'r') text = fr.read() fr.close() print(text)
4. mode of 'w': open for writing, truncating the file first
fw = open('example.txt', 'w') fw.write('hello! ') fw.write('this is a first file created by python! ')
fw.close()