Loop Control Statements in C
Introduction to HTML
How to use the Github API
The image tag, anchor tag and the button tag
Ordered and Unordered Lists in HTML
The division tag
HTML Forms
Tables in HTML
Introduction to C Programming
Introduction to Python
Varibles and Datatypes in Python
Operators in Python
Typecasting in Python
Input and Output in Python
If Else in Python
Loops in Python
Break, Continue and Pass in Python
Python practice section 1
Lists in Python
Tuple in Python
File Handling in Python
File handling in Python means creating, reading writing, and updating files through python
We can handle files of various formats like *.txt, *.csv, etc.
- Opening a file
- Reading a file
- Writing in a file
- Appending a file
Jump to specific sections
Opening a file
The open() function allows to open files and then we can read, write or update their content
as per our need. It takes two parameters.
- The complete path of the file
- The mode in which you want to open the file. By default, it is opened in 'r' mode if we don't specify any mode. (Optional)
Mode | Use |
---|---|
'r' | For reading the contents of the file only. Cannot change it. |
'w' | For writing in the file. Overwrites any existing content of the file. |
'a' | For appending at the end of the file. Does not overwrite the existing content of the file. If the file does not exist, then create a new file and then append it. |
'x' | For creating a new file. If the file already exists, then it throws an error. |
'+' | For both reading and writing a file. |
't' | Opens the file in text mode.(default) |
'b' | Opens the file in binary mode. |
# If the file is present in the current directory, just specify its name.
f=open("myfile.txt", 'r')
# Otherwise, specify its full path along with the name of the file like -
# f=open("C:\Users\HP\OneDrive\Desktop\myfile.txt",'r')
# When you open a file you must close it after your work is done.
f.close()
Alternate way of opening a file is by using the with keyword is given below. It automatically
closes the file after the control goes out of the indent of the with block. Hence, this method
is very useful.
with open("myfile.txt", 'r') as f:
pass
Reading a file
The read() function helps to read the whole content of the file.
with open('myfile.txt', 'r') as f:
print(f.read())
Output
Rapid Coders is a website where you will find courses, articles and projects related to various languages. It is an all-rounder platform for Computer Science.
The readlines() function returns a list of all the lines
present in the file.
with open('myfile.txt', 'r') as f:
print(f.readlines())
Output
['Rapid Coders is a website where you will find courses, articles, and projects related to various languages.\n', 'It is an all-rounder platform for Computer Science. ']
The readline() function reads a line one by one from the file.
with open('myfile.txt', 'r') as f:
while True:
line=f.readline()
if not line:
break
print(line)
Output
Rapid Coders is a website where you will find courses, articles, and projects related to various languages. It is an all-rounder platform for Computer Science.
Writing in a file
To overwrite a file, first open it in write mode and then use the write() function to write content
in it. Then again open the file in read mode and read its new content.
with open('myfile.txt', 'w') as f:
f.write("Overwriting myfile.txt")
with open('myfile.txt', 'r') as f:
print(f.read())
Output
Overwriting myfile.txt
The writelines() function is used to write multiple lines in the file.
lines=['Hello everyone\n','Hello rapid coders\n']
with open('myfile.txt', 'w') as f:
f.writelines(lines)
with open('myfile.txt', 'r') as f:
print(f.read())
Output
Hello everyone Hello rapid coders
Appending a file
print("Before appending the contents of the file are :\n")
with open('myfile.txt', 'r') as f:
print(f.read())
with open('myfile.txt', 'a') as f:
f.write("What a beautiful day!")
print()
print("After appending contents of the file are :\n")
with open('myfile.txt', 'r') as f:
print(f.read())
Output
Before appending the contents of the file are : Hello everyone Hello rapid coders After appending contents of the file are : Hello everyone Hello rapid coders What a beautiful day!