Character Meaning --------- --------------------------------------------------------------- 'r' open for reading (default) 'w' open for writing, truncating the file first 'x' create a new file and open it for writing 'a' open for writing, appending to the end of the file if it exists 'b' binary mode 't' text mode (default) '+' open a disk file for updating (reading and writing) 'U' universal newline mode (deprecated)
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#读取整个文件 file.read()
#读取文件前100个字符 file.read(100)
#按行读取文件 file.readline()
#读取所有行到一个列表中 file.readlines()
#写入文件 file.write('sadfasd')
1 2 3 4
#使用with语句打开文件,语句结束后会自动关闭文件 with open('1.txt') as file: for line in file: print(line)