Mode of File Access :
M3-R5.1 · Chapter 7: File Processing · 5 min read
File Access in Various Mode: The access mode determines the mode in which the file has to be opened. Like that read, write, append etc.
|
Access Mode |
Description |
|
'r' |
Opens file for reading only. This is the default, Mode. and file pointer at beginning of the file |
|
'w' |
Opens a file for writing only. Overwrites the file if already exists, else created a new file. The file pointer is at the beginning of the file |
|
'a' |
Opens a file for appending. The file pointer is at the end of the file if the file exists. if the file does not exist, it creates a new file for writing |
|
'r+' |
Opens file for both reading and writing mode .The file pointer exists at the beginning of the file. And this will not delete the content of the file |
|
'w+' |
Opens file for both reading and writing mode. It overwrite the previously written file.it create new file if no file exists |
|
'a+' |
Opens file for both reading and appending mode. It creates a new file if no file exists with the same name for reading and writing |
|
'rb' |
Open a file for both reading only in binary format. The file pointer exists at the beginning of the file |
|
'wb' |
Open a file for writing only in binary format. The file pointer exists at the beginning of the file |
|
'ab' |
Opens a file for appending only in binary format. It creates a new file in binary format if no file exists with the same name. |
|
'rb+' |
It opens the file for reading and writing both in binary format. |
|
'wb+' |
It opens the file for write and read both in binary format. |
|
'ab+' |
Opens a file to append and read both in binary format. It creates a new file in binary format if no file exists with the same name. |
Opening a file: A file operation starts with the file opening. At first, open the File then Python will start the operation. To open a file in Python, we use the open () function. The open () function returns a file object, and it typically takes two arguments: the file path and the mode in which you want to open the file (e.g., read mode, write mode, append mode, etc.).
There are two methods to open a file
-
- Using open() function B) Using with statement
Syntax: Using open() function (# this file in current working Directory):
Example:
f=open(“jai.txt”,r)
f=open(“C:\\Users\\Naitik\\Desktop\\jai.txt”,r)
f=open(r“C:\Users\Naitik\Desktop\jai.txt”,r)
Note: The prefix r in form of a string makes it raw string that means there is no special meaning attached to any character
The default file open mode is read mode , ie. If we do not provide any file open mode, python will open it in read mode.
Examples:
f=open("jai.txt","w")
f.write ("Welcome to my python programming….")
f.close()
print("I have written data in a file")
f=open("jai.txt")
print(f.read())
f.close()
More Examples is given below:
|
name=input("Enter your Name") age=input("enter your age:") f=open("jai.txt","w") f.write(name) f.write(age) f.close() |
#To read the written content in a file f=open("jai.txt") # if text file is already exit print(f.read()) f.close() |
|
f=open("jai.txt","w") f.write("Python is important for you upcoming Exam.") f.write("so we serious and do hard work on python") f.close() |
f=open("jai.txt") print(f.read()) f.close() **************************** f=open("jai.txt") print(f.read(5)) f.close() |
Using with Statement: This method is very handy when we have two related operations which we would like to execute as pair, with a block of code in between.
Syntax: With open (<filename with extension>,<FileMode>) as <File Handle>)
Benefits of using with statement
- It automatically closes the file after nested block of code
- It also handles all the exceptions also occurred before the end of block.
Method 2: Using With Statement……
with open("jai1.txt","w") as f:
f.write ("Welcome to jpm python programming….")
print("I have written data in a file")
f=open("jai1.txt")
print(f.read())
f.close()