Python provides built-in functions for creating, writing, and reading files. There are two types of files that can be processed in Python, plain text files and binaries (written in binary, 0 and 1).
- Text files: in In this type of file, each line of text is terminated with a special EOL (end of line) character, which is the default newline character (& # 39; / n & # 39;) in python.
- Binaries: there is no line separator in this file type, and the data is preserved after converting it to machine-readable binary.
In this article, we will focus on opening, closing, reading and writing data to text file.
File access modes
Access modes determine the type of possible operations in an open file. This refers to how the file will be used after opening it. These modes also determine the location of the file descriptor in the file. A file descriptor is like a cursor that defines where data should be read from or written to the file. Python has 6 access modes.
- Read only (& # 39; r & # 39;): open a text file for reading. The descriptor is located at the beginning of the file. If the file does not exist, an I / O error occurs. This is also the default mode in which the file is opened.
- Read and Write (& # 39; r + & # 39;): open the file for reading and writing. The descriptor is located at the beginning of the file. Throws an I / O error if the file does not exist.
- Write only (& # 39; w & # 39;): open file for writing. For an existing file, data is truncated and overwritten. The descriptor is located at the beginning of the file. Creates a file if the file does not exist.
- Write and Read (& # 39; w + & # 39;) : Open the file for reading and writing. For an existing file, data is truncated and overwritten. The descriptor is located at the beginning of the file.
- Append Only (& # 39; a & # 39;) : Open the file for writing. The file is created if it doesn’t exist. The descriptor is located at the end of the file. The data to be written will be inserted at the end, after the existing data.
- Add and Read (& # 39; a + & # 39;): open the file for reading and writing. The file is created if it doesn’t exist. The descriptor is located at the end of the file. The data being written will be inserted at the end, after the existing data.
Opening a file
This is done using the open () function. This function does not require a module import.
File_object = open (r "File_Name", "Access_Mode")
The file must exist in the same directory as the python program file, in Otherwise, the full address of the file must be written in place of the file name.
Note: r is placed before the filename to prevent characters in the filename string from being treated as special characters. For example, if a file contains / temp, then / t is treated as a tab character and an invalid address error occurs. R makes the string unprocessed, that is, it says that the string has no special characters. The r character can be ignored if the file is in the same directory and the URL is not located.
|
Here file1 is created as an object for MyFile1 and file2 as an object for MyFile2
Closing the file
The close () function closes the file and frees the memory space allocated by this file. It is used when the file is no longer needed, or when it needs to be opened in a different file mode.
File_object.close ()
|
Write to file
There are two ways to write to file.
- write (): inserts line str1 on one line in a text file.
File_object.write (str1)
- writelines (): s trong> for a list of string items, each line is inserted into a text file. Used to insert multiple lines at the same time.
File_object.writelines (L) for L = [str1, str2, str3]
Read from file
There are three ways to read data from a text file.
- read (): returns the bytes read as a string. Reads n bytes, if n is not specified, reads the entire file.
File_object.read ([n])
- readline (): reads a line from a file and returns as a string. For the specified n, reads at most n bytes. However, it does not read more than one line, even if n is longer than the line length.
File_object.readline ([n])
- readlines (): reads all lines and returns them as each line as a line item in the list.
File_object.readlines ()
Note: & # 39; / n & # 39; treated as a two-byte special character
|
Output:
Ou tput of Read function is Hello This is Delhi This is Paris This is London Output of Readline function is Hello Output of Read (9) function is Hello Th Output of Readline (9) function is Hello Output of Readlines function is [’Hello’, ’This is Delhi’, ’This is Paris’, ’This is London’]
Attach to file
< p> file1 = open ( "myfile.txt" , "r" ) |
Output:
Output of Readlines after appending [’This is Delhi’, ’This is Paris’, ’This is London’, ’Today’] Output of Readlines after writing [’Tomorrow’]
Related article: Latest questions
Shop