Psc-Unit 1-4-File Handling in Python
Psc-Unit 1-4-File Handling in Python
In Python, �les can broadly be categorized into two types based on their mode of operation:
The four primary functions used for �le handling in Python are:
2 of 9 7/24/24, 15:00
file handling in python.ipynb - Colab https://colab.research.google.com/drive/1hdfFMqXwfU_s...
f = open(�lename, mode)
Where the following mode is supported:
r+: To read and write data into the �le. This mode does not
override the existing data, but you can modify the data starting
from the beginning of the �le.
w+: To write and read data. It overwrites the previous �le if one
exists, it will truncate the �le to zero length or create a �le if it
does not exist.
a+: To append and read data from the �le. It won’t override
existing data.
There is more than one way to How to read from a �le in Python. Let us see how we can read
the content of a �le in read mode.
Example 1: The open command will open the Python �le in the
read mode and the for loop will print each line present in the �le.
import os
def create_file(filename):
try:
with open(filename, 'w') as f:
f.write('Hello, world!.....\nwelcome to Indus University \nthis is example
print("File " + filename + " created successfully.")
except IOError:
print("Error: could not create file " + filename)
if __name__ == '__main__':
filename = "example.txt"
#new_filename = "example.txt"
create_file(filename)
File example.txt created successfully.
4 of 9 7/24/24, 15:00
file handling in python.ipynb - Colab https://colab.research.google.com/drive/1hdfFMqXwfU_s...
print (each)
Hello, world!.....
Hello, world!.....
welcome to Indus University
this is example of python file handling
print(data)
Hello, world!.....
welcome to Indus University
this is example of python file handling
5 of 9 7/24/24, 15:00
file handling in python.ipynb - Colab https://colab.research.google.com/drive/1hdfFMqXwfU_s...
Hello
['Hello,', 'world!.....']
['welcome', 'to', 'Indus', 'University']
['this', 'is', 'example', 'of', 'python', 'file', 'handling']
Example 1: In this example, we will see how the write mode and
the write() function is used to write in a �le. The close()
command terminates all the resources in use and frees the
system of this particular program.
print(data)
6 of 9 7/24/24, 15:00
file handling in python.ipynb - Colab https://colab.research.google.com/drive/1hdfFMqXwfU_s...
import os
def create_file(filename):
try:
with open(filename, 'w') as f:
f.write('Hello, world!\n')
print("File " + filename + " created successfully.")
except IOError:
print("Error: could not create file " + filename)
def read_file(filename):
try:
with open(filename, 'r') as f:
contents = f.read()
print(contents)
except IOError:
print("Error: could not read file " + filename)
7 of 9 7/24/24, 15:00
file handling in python.ipynb - Colab https://colab.research.google.com/drive/1hdfFMqXwfU_s...
def delete_file(filename):
try:
os.remove(filename)
print("File " + filename + " deleted successfully.")
except IOError:
print("Error: could not delete file " + filename)
if __name__ == '__main__':
filename = "example.txt"
#new_filename = "new_example.txt"
create_file(filename)
if __name__ == '__main__':
filename = "example.txt"
#new_filename = "new_example.txt"
read_file(filename)
Hello, world!
if __name__ == '__main__':
filename = "example.txt"
new_filename = "new_example.txt"
8 of 9 7/24/24, 15:00
file handling in python.ipynb - Colab https://colab.research.google.com/drive/1hdfFMqXwfU_s...
9 of 9 7/24/24, 15:00