To open and close a file:
Python provides the necessary functions and methods for basic file operation by default.
Open function
You must first open a file with the Python built-in open () function, create a Document object, and the related method can call it for read and write.
Grammar:
File Object = open (file_name [, access_mode][, Buffering])
The details of each parameter are as follows:
The ①file_name:file_name variable is a string value that contains the name of the file you want to access.
②access_mode:access_mode determines the mode of opening the file: read-only, write, append, etc. All the desirable values are shown in the full list below. This parameter is non-mandatory and the default file access mode is read-only (R).
③buffering: If the value of buffering is set to 0, there is no deposit. If the value of buffering is 1, the row is stored when the file is accessed. If you set the value of buffering to an integer greater than 1, it indicates that this is the buffer size of the storage area. If a negative value is taken, the buffer size of the storage area is the system default.
Full list of open files in different modes:
Mode
|
Describe
|
R
|
Open the file as read-only. The pointer to the file will be placed at the beginning of the file. This is the default mode. |
Rb
|
Opens a file in binary format for read-only. The file pointer will be placed at the beginning of the file. This is the default mode. |
r+
|
Open a file for read-write. The file pointer will be placed at the beginning of the file. |
rb+
|
Opens a file in binary format for read-write. The file pointer will be placed at the beginning of the file. |
W
|
Open a file for writing only. Overwrite the file if it already exists. If the file does not exist, create a new file. |
Wb
|
Open a file in binary format only for writing. Overwrite the file if it already exists. If the file does not exist, create a new file. |
w+
|
Open a file for read-write. Overwrite the file if it already exists. If the file does not exist, create a new file. |
wb+
|
Opens a file in binary format for read-write. Overwrite the file if it already exists. If the file does not exist, create a new file. |
A
|
Opens a file for appending. If the file already exists, the file pointer will be placed at the end of the file. In other words, the new content will be written to the existing content. If the file does not exist, create a new file to write to. |
Ab
|
Opens a file in binary format for appending. If the file already exists, the file pointer will be placed at the end of the file. In other words, the new content will be written to the existing content. If the file does not exist, create a new file to write to |
A +
|
Open a file for read-write. If the file already exists, the file pointer will be placed at the end of the file. The file opens with an append mode. If the file does not exist, create a new file to read and write. |
ab+
|
Opens a file in binary format for appending. If the file already exists, the file pointer will be placed at the end of the file. If the file does not exist, create a new file to read and write. |
X
|
Open throws an exception if the file exists
|
After a file is opened, you have a files object that you can get various information about the file.
The following is a list of all properties related to the file object:
F.closed ()
|
Returns true if the file has been closed, otherwise false.
|
F.mode ()
|
Returns the access mode of the file being opened |
F.name ()
|
Returns the name of the file. |
F.softspace ()
|
If the print output must be followed by a space character, false is returned. Otherwise, returns True. |
|
Close () method
The close () method of the file object flushes any information that has not yet been written in the buffer and closes the file, which can no longer be written. When a reference to a file object is re-assigned to another file, Python closes the previous file. Closing a file with the close () method is a good habit.
Grammar:
Fileobject.close ()
# Opens a File F1 = open ("F.txt", "W") print "file name:", F1.name # Close Open File F1.close ()
Read and Write files:
The file object provides a series of methods that make it easier to access our files. Take a look at how to read and write to the file using the read () and write () methods.
Read () method
The Read () method reads a string from an open file. It is important to note that the Python string can be binary data, not just text.
Grammar:
Fileobject.read ([Count]) # #无参数, read all; parameters, (b, by byte | No B, by character)
The passed parameter is the count of bytes to read from the open file. The method reads from the beginning of the file, and if it does not pass in count, it tries to read as much of the content as possible until the end of the file.
File location
The tell () method tells you the current position within the file, gets the current pointer position, and the next read and write occurs after so many bytes at the beginning of the file.
The Seek (offset [, from]) method changes the position of the current file. The offset variable represents the number of bytes to move. The from variable specifies the reference position at which to begin moving bytes.
If from is set to 0, this means that the beginning of the file is used as the reference location for moving bytes. If set to 1, the current position is used as the reference location. If it is set to 2, then the end of the file will be used as the reference location.
Write () method
The write () method writes any string to an open file. It is important to note that the Python string can be binary data, not just text.
The Write () method does not add a line break at the end of the string (' \ n ')
Grammar:
Fileobject.write (String)
The passed argument is the content to be written to the open file, with a B, a write section, and no B, and write characters.
Flush () method
Force flush file internal buffer, write into file
Functions commonly used by file objects:
Method
|
Describe
|
F.close ()
|
Close the file. The file can no longer read and write after closing. |
F.flush ()
|
Refreshes the file internal buffer, directly writes the internal buffer data immediately to the file, rather than passively waits for the output buffer to write. |
F.fileno ()
|
Returns an integer that is a file descriptor (Descriptor FD Integer) that can be used in some of the underlying operations, such as the Read method of the OS module. |
F.isatty ()
|
Returns True if the file is connected to an end device, otherwise False is returned. |
F.next ()
|
Returns the next line of the file. |
F.read ([size])
|
Reads the specified number of bytes from the file, if none is given or is negative. |
F.readline ()
|
Reads the entire line, including the "\ n" character. |
F.seek (offset[, whence])
|
Set the current location of the file |
F.tell ()
|
Returns the current location of the file. |
F.truncate ([size])
|
Intercepts the file, the truncated byte is specified by size, and the default is the current file location. |
F.write (str)
|
Writes a string to the file without a return value.
|
F.writelines (Sequence)
|
Writes a list of sequence strings to the file and adds a newline character to each line if a line break is required.
|
Open file with mode
With open (' db ') as F1, open (' DB2 ') as F2:pass
This article is from the "LE" blog, please be sure to keep this source http://lz001.blog.51cto.com/8294567/1795399
Python Learning-File manipulation