procedure for file operation
- Open the file, get the file handle and assign a value to a variable
- Memory object of the file-contains file name, character set, size, start and end position on the hard disk ...
- Manipulating a file with a handle
- Close File
Open Method
The open () function opens a file and creates a Files object with the following syntax:
File Object = open (file_name [, access_mode][, Buffering])
- 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. 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.
- In addition, there may be parameters that are character sets: Encoding= "Utf-8"
Open mode:
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. |
File Object
After a file is opened, you have a Files object with the following properties:
- file.closed----> Returns True if the file has been closed, otherwise false is returned.
- File.mode----> Returns the access mode of the file being opened.
- File.name----> Returns the name of the file.
- File.softspace---> returns false if the print output must be followed by a space character. Otherwise, returns True.
Close () method-----> Fileobject.close ()
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.
Read () method
The Read () method reads a string from an open file. It is important to note that thePython string can be binary data , not just text.
Fileobject.read ([Count])
The passed parameter is the count of bytes to read from the open file. The method reads in from the beginning of the file, and if it does not pass in Count, it tries to read as much more content as possible, most likely until the end of the file.
Write () method
- The write () method writes any string to an open file.
- Python strings can be binary data, not just text.
- The Write () method does not add a line break (' \ n ') at the end of the string:
Fileobject.write (String)
Python4-File operations