21
loading...
This website collects cookies to deliver better user experience
r mode is the default when no mode passed, used to read file, it throws FileNotFoundError if the file passed in first argument does not exist.
w mode is write data into a file, if data exist in the file it removes that data then, start writing the data into the file , if the filename passed does not exist,it creates a new file.
a mode write data to existing file, creates a file if it does not exist.
delete file with specified filename using os module ,if the file does not exist it throws FileExistsError error.
b in Binary mode.
For Example :-
file1 = open("file.txt",'wb') # means write in binary mode
file2 = open("file.txt",'wt') # means write in text mode
Name : Ram Kumar
Age : 13
City : Ajmir
Name : Alice Pen
Age : 12
City : Boston
Name : Marcus Lee
Age : 15
City : Tokyo
file = open("students.txt",'r')
for line in file:
print(line,end="")
file.close()
Output :-
Name : Ram Kumar
Age : 13
City : Ajmir
Name : Alice Pen
Age : 12
City : Boston
Name : Marcus Lee
Age : 15
City : Tokyo
file = open("students_2.txt",'w')
student_info = {
"Name" : "Mike Javen",
'Age' : 15,
'City' : 'Minisoda'
}
for field,data in student_info.items():
file.write(f"{field} : {data} \n")
file.close()
students_2.txt file content :-
Name : Mike Javen
Age : 15
City : Minisoda
file = open("students.txt",'a')
students_info = [ {
"Name" : "John Lenon",
'Age' : 14,
'City' : 'Navi Mumbai'
},
{
"Name" : "Sam Dune",
'Age' : 11,
'City' : 'Boston'
}
]
for student_info in students_info:
file.write("\n")
for field,data in student_info.items():
file.write(f"{field} : {data} \n")
file.close()
Students.txt file.content :-
Name : Ram Kumar
Age : 13
City : Ajmir
Name : Alice Pen
Age : 12
City : Boston
Name : Marcus Lee
Age : 15
City : Tokyo
Name : John Lenon
Age : 14
City : Navi Mumbai
Name : Sam Dune
Age : 11
City : Boston
import os
filename = 'students_2.txt'
os.remove(filename)
print(f"successfully deleted file {filename}")
Output :-
successfully deleted file students_2.txt
with open("students.txt",'r') as file:
for line in file:
print(line, end="")
Output :-
Name : Ram Kumar
Age : 13
City : Ajmir
Name : Alice Pen
Age : 12
City : Boston
Name : Marcus Lee
Age : 15
City : Tokyo
Name : John Lenon
Age : 14
City : Navi Mumbai
Name : Sam Dune
Age : 11
City : Boston
file = open("new_students.txt",'wb')
students_info = b'''
"Name" : "Sam Dune",
'Age' : 11,
'City' : 'Boston'
}
'''
file.write(students_info)
file.close()
Output :-
"Name" : "Sam Dune",
'Age' : 11,
'City' : 'Boston'
# file content :-
#my name is john levi and i
#live in boston and study in
#xyz univeristy doing majors
#in computer science.
person_info = [line for line in open('info.txt')]
for line in person_info:
print(line,end="")
Output :-
my name is john levi and i
live in boston and study in
xyz univeristy doing majors
in computer science.
# File Content :-
# Ram Kumar , 12 , Indore
# Shayam Kumar, 13 , Mumbai
# Ajay Kumar, 11 , Delhi
# Harris javen, 15 , New Jersey
students_info = []
for line in open("info.txt"):
name, age, city = line.split(',')
student = {
"Name" : name.strip(),
"Age" : age.strip(),
"City" : city.strip()
}
students_info.append(student)
print("Content tetched from file and stored in dictionary\n")
for student in students_info:
print(student)
Output :-
Content tetched from file and stored in dictionary
{'Name': 'Ram Kumar', 'Age': '12', 'City': 'Indore'}
{'Name': 'Shayam Kumar', 'Age': '13', 'City': 'Mumbai'}
{'Name': 'Ajay Kumar', 'Age': '11', 'City': 'Delhi'}
{'Name': 'Harris javen', 'Age': '15', 'City': 'New Jersey'}