Python's function to read and write files is open or file
File_handler = open (Filename,,mode)
Table mode
Mode |
Describe |
R |
Open the file as read to read the file information. |
W |
Open a file in write mode to write information to the file. If the file exists, empty the file, and then write the new content |
A |
Open a file in Append mode (that is, an open file, the file pointer is automatically moved to the end of the file), and if the file does not exist, create |
r+ |
The file can be read and written by opening the file in read/write mode. |
w+ |
Remove the file contents, and then open the file as read-write. |
A + |
Open the file in read-write mode and move the file pointer to the end of the file. |
B |
Open the file in binary mode instead of in text mode. This mode is only valid for Windows or DOS, and Unix-like files are operated in binary mode. |
Table File Object Methods
Method |
Describe |
F.close () |
Close the file, remember to open the file after opening () must remember to close it, otherwise it will occupy the system open file handle number. |
F.fileno () |
Gets the file descriptor, which is a number |
F.flush () |
Refresh Output Cache |
F.isatty () |
Returns true if the file is an interactive terminal, otherwise false. |
F.read ([Count]) |
Reads the file, and if there is count, it reads count bytes. |
F.readline () |
Read a line of information. |
F.readlines () |
Reads all the lines, that is, the information that reads the entire file. |
F.seek (Offset[,where]) |
Move the file pointer to the offset position relative to where. Where 0 indicates the beginning of the file, this is the default, 1 is the current position, and 2 means the end of the file. |
F.tell () |
Gets the file pointer position. |
F.truncate ([size]) |
Intercepts the file so that the file size is sized. |
F.write (String) |
Writes string strings to a file. |
F.writelines (list) |
Writes a string in a list to a file in a row, and writes the file continuously without wrapping. |
Read the file
Read = open (Result)
Line=read.readline ()
While line:
Print Line
Line=read.readline () #如果没有这行会造成死循环
Read.close
Write a file
Read = File (result, ' A + ')
Read.write ("\ r \ n")
Read.write ("Thank You")
Read.close
Other
#-*-encoding:utf-8-*-filehandler = open (' C:\\111.txt ', ' R ') #以读方式打开文件, RB is binary (tablet or executable file, etc.)
print ' Read () function: ' #读取整个文件 print Filehandler.read ()
print ' ReadLine () function: ' #返回文件头, read one line filehandler.seek (0) print filehandler.readline ()
print ' ReadLines () function: ' #返回文件头, returns a list of all rows Filehandler.seek (0) print filehandler.readlines ()
print ' List all lines ' #返回文件头, show All Rows Filehandler.seek (0) textlist = filehandler.readlines () for line in text List:print Line, print print
print ' Seek function ' #移位到第15个字符 to display the remainder from 16 characters filehandler.seek () print ' Tell () function ' Print Fileh Andler.tell () #显示当前位置 print filehandler.read ()
Filehandler.close () #关闭文件句柄
Python file operations