The road to Python learning - file manipulation

  File operations are divided into three steps: open the file, read and write the file, and close the file. If you do not add parentheses to the read function during the read operation, the following car accident will occur

1 >>> data = open('/home/supersun/Documents/FullStack/Week1/helloworld',encoding='utf-8')
2 >>> print(data.read)
3 <built-in method read of _io.TextIOWrapper object at 0x7f4bcc08b708>
4 >>> print(data.read())
5 hello world!
6 hello world!
7 hello world!
8 hello world!
9 hello world!

  The function open takes the filename as the only essential parameter and returns an object. The parameter mode of the function open defaults to the 'rt' read operation.

 

Common values ​​of the parameter mode of the function open

 value    
Depiction
 'r'   
Read mode (default). FileNotFoundError is raised when the file does not exist . An error will be reported when writing to the file.
 'w'  write mode. A new file is created when the file does not exist, and when the file is opened, the existing content is deleted and writing starts from the beginning of the file. An error will be reported when reading the file.
 'a'  Append mode, creates a new file if it doesn't exist, appends content to the end of the file.
'x' Exclusive write mode, creates a new file if the file does not exist , raises FileExitsError if the file already exists . An error will be reported when reading the file.
't' Text mode (default), used in conjunction with other modes.
'b' Binary mode (used in combination with other modes). If the file contains non-text binary data, such as sound clips or images, use binary mode (eg 'rb'/'wb'/'ab'/'rb+'/'wb+'/'ab+') to disable text related functions.
'+'  Read-write mode (used in conjunction with other modes).
'r+'  Read and write. FileNotFoundError is raised when the file does not exist . When opening a file, the cursor defaults to the beginning of the file. In the beginning, no matter reading or writing, operations are performed from the beginning of the file. After that, each read and write operation starts at the cursor position after the previous operation.
 'w+'  Read and write, create a new file if it doesn't exist . When the file is opened, the existing content is deleted and writing is started from the beginning of the file. ps: just opened the file, the read content will be empty
 'a+'  Read and write, create a new file if it doesn't exist. When opening a file, the cursor is at the end of the file, and both reading and writing files add content from the end. ps: So there is nothing when reading first, because the cursor is at the end.

  During the reading operation, whether it is a letter or a Chinese character, it is read as a unit symbol.

  An important function of file.close() is to load the contents of the buffer into the disk when the file is closed. Of course, even if file.close() is omitted or not written, the Python interpreter will automatically close the file for us, but Not sure when to close the file, so it is recommended to close the file explicitly with code.

  Two Python programs can operate on a file at the same time.

file.read()

file.readline()

file.readlines() #Return a list, each line represents an element.

file.flush() #Update the data in the buffer area to the current file immediately

Using the with statement can save the file closing operation. The file operation is written within the scope of the with statement. When the with statement is left, the program will automatically close the file, and two files can be opened at the same time.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324691065&siteId=291194637