![]() |
In Python, the file mode specifies the purpose and the operations that can be performed on a file when it is opened. When you open a file using the open() function, you can specify the file mode as the second argument. Different File Mode in PythonBelow are the different types of file modes in Python along with their description:
File Mode in PythonBelow are some of the file modes in Python: Read Mode (‘r’) in PythonThis mode allows you to open a file for reading only. If the file does not exist, it will raise a FileNotFoundError. example.txt Hello Geeks Example: In this example, a file named ‘example.txt’ is opened in read mode (‘r’), and its content is read and stored in the variable ‘content’ using a ‘with’ statement, ensuring proper resource management by automatically closing the file after use.
Output: Hello Geeks Write Mode (‘w’) in PythonThis mode allows you to open a file for writing only. If the file already exists, it will truncate the file to zero length. If the file does not exist, it will create a new file. example.txt Hello, world! Example: In this example, a file named ‘example.txt’ is opened in write mode (‘w’), and the string ‘Hello, world!’ is written into the file.
Output: Hello, world! Note – If you were to open the file “example.txt” after running this code, you would find that it contains the text “Hello, world!”. Append Mode (‘a’) in PythonThis mode allows you to open a file for appending new content. If the file already exists, the new content will be added to the end of the file. If the file does not exist, it will create a new file. example.txt Hello, World!
This is a new line Example: In this example, a file named ‘example.txt’ is opened in append mode (‘a’), and the string ‘\nThis is a new line.’ is written to the end of the file.
Output: Hello, World!
This is a new line The code will then write the string “\nThis is a new line.” to the file, appending it to the existing content or creating a new line if the file is empty. Binary Mode (‘b’) in PythonThis mode use in binary files, such as images, audio files etc. Its always used by combined with read (`’rb’`) or write (‘wb’) modes. Example: In this example, a file named ‘image.png’ is opened in binary read mode (‘rb’). The binary data is read from the file using the ‘read()’ method and stored in the variable ‘data’.
Read and Write Mode (‘r+’) in PythonThis mode allows you to open a file for both reading and writing. The file pointer will be positioned at the beginning of the file. If the file does not exist, it will raise a FileNotFoundError. example.txt This is a new line.
Initial content.
Output: If the initial contents of “example.txt” we are “Initial content.”, after running this code, the new content of the file would be: This is a new line.
Initial content. Write and Read Mode (‘w+’) in PythonThis mode allows you to open a file for both reading and writing. If the file already exists, it will truncate the file to zero length. If the file does not exist, it will create a new file. example.txt Hello, world! Example: In this example, a file named ‘example.txt’ is opened in write and read mode (‘w+’).
Output: Therefore, the output of this code will be the string “Hello, world!”. Since the file was truncated and the pointer was moved to the beginning before reading, the contents of the file will be exactly what was written to it. So, content will contain the string “Hello, world!”. Hello, world! |
Reffered: https://www.geeksforgeeks.org
Python |
Related |
---|
![]() |
![]() |
![]() |
![]() |
![]() |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 14 |