file handling - Jupyter Notebook 27-01-2025
file handling - Jupyter Notebook 27-01-2025
File Handling
A file is a named location used for storing data. For example, main.py is a file that is always
used to store Python code.
Python provides various functions to perform different file operations, a process known as
File Handling.
Opening Files
Reading Files
Writing Files
Closing Files
"w" - Write - Opens a file for writing, creates the file if it does not exist
"x" - Create - Creates the specified file, returns an error if the file exists
"a" - Append - Opens a file for appending, creates the file if it does not exist
In [11]: 1 file=open("testing.txt","r")
In [12]: 1 content=file.read()
In [14]: 1 print(content)
Reading Files
Writing Files
Closing Files
In [17]: 1 f=open("try_file.txt","r")
In [19]: 1 print(f.read())
In [21]: 1 f.close()
--------------------------------------------------------------------------
-
ValueError Traceback (most recent call las
t)
Input In [23], in <cell line: 1>()
----> 1 f.read()
62
In [36]: 1 f=open("try_file.txt")
2 f.seek(24)
3 print(f.read())
In [38]: 1 print(f.tell())
62
In [40]: 1 f.close()
If you have opened the file in read mode, then you cannot perform write operation on it.
If you have opened the file in write mode, then you cannot perform read operation on it.
Here, with...open automatically closes the file, so we don't have to use the close()
function.
In [43]: 1 f=open("test.txt","w")
2 f.write("sql is a structured query language. \n It is used for maintain
3 f.close()
In [46]: 1 f=open("test.txt","r")
2 print(f.read())
In [48]: 1 f.close()
In [51]: 1 f=open("excel.txt","w")
2 f.write("excel is used for creating and maintaing the report. \n Excel
3
4
5 f.close()
In [53]: 1 f=open("excel.txt","r")
2 print(f.read())
3 f.close()
"a" - Append - will create a file if the specified file does not exist
"w" - Write - will create a file if the specified file does not exist
In [58]: 1 f=open("text.txt","a")
2 f.write("\n It has 5 command DDL, DML, DQL, TCL, DCL. \n there are some
3 f.close()
In [59]: 1 f=open("text.txt","r")
2 print(f.read())
3 f.close()
In [1]: 1 f=open("powerbi.txt","a")
2 f.write("power bi is visualization tool")
3 f.close()
In [2]: 1 f=open("powerbi.txt","r")
2 print(f.read())
In [65]: 1 f=open("excel.txt","x")
2 f.write("hello excel")
3 f.close()
--------------------------------------------------------------------------
-
FileExistsError Traceback (most recent call las
t)
Input In [65], in <cell line: 1>()
----> 1 f=open("excel.txt","x")
2 f.write("hello excel")
3 f.close()
In [68]: 1 f=open("excel2.txt","x")
2 f.write("hello excel")
3 f.close()
In [70]: 1 f=open("excel2.txt","r")
2 print(f.read())
hello excel
In [72]: 1 f.close()
In [75]: 1 # r+ ===> read + write, if a file doesn't exist it will throw error wil
2
3 f=open("text.txt","r+")
4 print(f.read())
5 print("***********************************************")
6 f.write("\n sql has sub topics like sub quries and joins ")
7
8 f.seek(0)
9 print(f.read())
10 f.close()
In [77]: 1 """" w+ ===> write + read ===> mode will write the data
2 and create a new file if file doesn't exit otherwise replace the data"
3
4 f=open("text.txt","w+")
5 f.write("we are learning the file handling \n we are learning the diff
6 f.seek(0)
7 print(f.read())
8 f.close()
9
In [80]: 1 """ a+ mode ===> append + read ===> this mode will append the data in
2 and if file doesn't exist it will create a new file and the read the da
3
4 f = open("text.txt","a+")
5 f.write("\n our next topic is oops")
6 f.seek(0)
7 print(f.read())
8 f.close()
27-01-2025
In [3]: 1 # readline===> for single line
2 # readlines===> for multiple line
In [7]: 1 f=open("text.txt","r")
2 print(f.readline())
3 print(f.readline(10))
4 f.close()
we are le
In [11]: 1 f=open("text.txt")
2 print(f.readlines())
['we are learning the file handling \n', ' we are learning the different
modes of file handling \n', ' our next topic is oops\n', ' our next topic
is oops\n', ' our next topic is oops']
In [13]: 1 f=open("color.txt","r")
2 s=f.read()
3 l=s.split(" ") # to split a line into individual words
4 print(l)
5 print(len(l))
exception handling
In [15]: 1 # normally all the error are by default handled by python interpretor
2
3 a=2
4 b=0
5 a/b
--------------------------------------------------------------------------
-
ZeroDivisionError Traceback (most recent call las
t)
Input In [15], in <cell line: 5>()
3 a=2
4 b=0
----> 5 a/b
In [17]: 1 try:
2 a=2
3 b=0
4 print(a/b)
5 except:
6 print("you are dividing int with 0")
In [20]: 1 num=[2,4,5,6]
2 print(num[0])
3 print(num[11])
--------------------------------------------------------------------------
-
IndexError Traceback (most recent call las
t)
Input In [20], in <cell line: 3>()
1 num=[2,4,5,6]
2 print(num[0])
----> 3 print(num[11])
In [22]: 1 try:
2 num=[2,4,5,6]
3 print(num[11])
4 except:
5 print("index is missing") # this will work only when there is error
6 else:
7 print("no error") # this will work when there is no error
index is missing
In [24]: 1 try:
2 num=[2,4,5,6]
3 print(num[2])
4 except:
5 print("index is missing")# this will work only when there is error
6 else:
7 print("no error") # this will work when there is no error
5
no error
In [26]: 1 try:
2 num=[2,4,5,6]
3 print(num[3])
4 except:
5 print("index is missing")# this will work only when there is error
6 else:
7 print("no error")# this will work when there is no error
6
no error
In [29]: 1 try:
2 num=[2,4,5,6]
3 print(num[0])
4 except:
5 print("index is missing")# this will work only when there is error
6 else:
7 print("no error")# this will work when there is no error
8 finally:
9 print("successfully completed")
2
no error
successfully completed
In [ ]: 1