Friday, February 17, 2023

Difference between r, r+, w, w+, a and a+ in Python

 

Differences between open modes r, r+, w, w+, a and a+ in open() function.


r r+ w w+ a a+
read * *
*
*
write
* * * * *
create

* * * *
truncate

* *

position at start * * * *

position at end



* *

In this context, truncate means delete the content of the file.

1.2 Definition of open modes r, r+, w, w, a, a+:

  • The r throws an error if the file does not exist or opens an existing file without truncating it for reading; the file pointer position at the beginning of the file.
  • The r+ throws an error if the file does not exist or opens an existing file without truncating it for reading and writing; the file pointer position at the beginning of the file.
  • The w creates a new file or truncates an existing file, then opens it for writing; the file pointer position at the beginning of the file.
  • The w+ creates a new file or truncates an existing file, then opens it for reading and writing; the file pointer position at the beginning of the file.
  • The a creates a new file or opens an existing file for writing; the file pointer position at the end of the file.
  • The a+ creates a new file or opens an existing file for reading and writing, and the file pointer position at the end of the file.

File Operations in Python

File operations are the operations that can be performed on a file. These include operations carried out by the user using Python commands (or any other programming language).

A few fundamental file operations are listed below:

  1. Open: The first and most important operation on a file is to open it. When you create a file, you must open it in order to do further file processing operations. Python offers an in-built open() function to open a file. The open() function returns a file object, also known as a handle, to perform further operations accordingly.
  2. Read: As the name suggests, this operation reads the content of a file. Python provides various methods to read a file, the most common being the read() function. Note that in order to read a file you'll need to open that file in 'read mode'.
  3. Write: This operation is used to write information into a file. There are various modes, that can be used, for the write operation (we'll soon discuss the different modes).
  4. Close: After completing all procedures, the file must be closed in order to save the data. This operation frees up all the resources used up by the file while processing. Python has a close() method to close the file.

Python has six File Access Modes:

Sr. No.

Access Mode

(File Operations)

Description
1. Read Only ('r')

Default mode. Opens a file in Python to read. (Raises an I\O error if the file does not exist.)

2. Read & Write ('r+') With this, you can read as well as write in the file.
3. Write Only ('w') It is used to write in a file. (This creates a new file if the file doesn't exist). This overwrites on an existing file.
4. Write & Read ('w+') Used for writing as well as reading an opened file in Python
5. Append Only ('a') This is used to insert data at the end of an opened file. Here, the existing data won't get truncated.
6. Append & Read ('a+') This is used to open a file for writing (at the end) and reading.

The access methods are mentioned along with the file name in the open() function.

The syntax to open a file is:

f = open("FilePath", "access mode")

 Using seek() and truncate() function

This method can be used to overwrite a file (completely or a particular line) in Python. This method involves two functions :

  1. The seek() function: This function sets the handler (pointer) at the beginning of the file. This is called upon to ensure that the handler stays at the start of the file, hence by default it is set at 0.
  2. The truncate() function: This function removes the data of the file.
""" File Content: 
Program: To Overwrite a File in Python
Overwriting a File : Replacing old contents of the file """

# open the file using write only mode
handle = open("file.txt", "w")

# seek out the line you want to overwrite
handle.seek(0)
handle.write("File Overwritten.")
handle.truncate()

# close the file
handle.close()

# To read the contains of the file
# open the file in read mode
f = open("file.txt", "r")
print(f.read())
f.close()
 
OR
 
# Program: Overwriting a File in Python
""" File Content: 
Program: To Overwrite a File in Python
Overwriting a File : Replacing old contents of the file """

# open the file using read only mode
handle = open("file.txt", "r")

# reading the file and storing the data in content
content = handle.read()
# replacing the data using replace()
content = content.replace("File", "Data")

# close the file
handle.close()

handle = open("file.txt", "w")
handle.write(content)
handle.close()

# To read the contains of the file
# open the file in read mode
f = open("file.txt", "r")
print(f.read())
f.close() 

 Source: favtutor.com