Filing in Python
Program 1
#open txt file in write mode
f1=open("[Link]","w")
[Link]("Welcome to kolkata")
print("data has been file up......")
[Link]()
Output
data has been file up......
Program 2
#open file in read mode
f1=open("[Link]","r")
f2=open("[Link]","w")
print([Link]())#read all data
print([Link]())#read multipal lines
print([Link](2))
[Link]()
Output
Welcome to kolkata
['file\n', 'pointer\n', 'if_else\n', 'loop\n', 'function\n']
1
Program 3
#Reading from file with help of bitposition
f1=open("[Link]","r")
print([Link](3))
[Link]()
Output
Wel
Program 4
#open txt file in append mode
f1=open("[Link]","a")
a=int(input("Enter number of string:- "))
for i in range(a):
k=input("Enter your string:- ")
[Link](k)
[Link]("\n")
[Link]()
Output
Enter number of string:- 2
Enter your string:- welcome
Enter your string:- kolkata
2
Program 5
#open bynary file
import pickle
f=open("[Link]","wb")#binary file open in write mode
num=[5,10,15,20,25]
arr=bytearray(num)#convert array to bytearray
[Link](arr)#data wirite in binary fie
[Link]()
f=open("[Link]","rb")#binary file opne in read mode
num=list([Link]())#date read from binary file
print(num)#data display in screen
[Link]()
Output
[5, 10, 15, 20, 25]
Program 6
import pickle
f=open("[Link]","wb")
num=[5,10,15,20,25]
num1=[15,110,151,20,25]
dict={1:"python",2:"C++"}
[Link](num,f)#with help of dump function data write in binary file
[Link](num1,f)
[Link](dict,f)
[Link]()
f=open("[Link]","rb")
try:
3
while True:
s=[Link](f)#with help of load function data read from file
print(s)
except EOFError:
[Link]()
Output
[5, 10, 15, 20, 25]
[15, 110, 151, 20, 25]
{1: 'python', 2: 'C++'}
Program 7
import pickle
f=open("[Link]","wb")
list1=[]
a=int(input("Enter any ending value:- "))
for i in range(1,a+1):
k=int(input("Enter any value:- "))
[Link](k)
[Link](list1,f)
f=open("[Link]","rb")
try:
while True:
s=[Link](f)
print(s)
except EOFError:
[Link]()
4
Output
Enter any ending value:- 4
Enter any value:- 11
Enter any value:- 30
Enter any value:- -9
Enter any value:- 13
[11, 30, -9, 13]
Program 8
#open multipla binary file
import pickle as p
f=open("[Link]","wb")
f1=open("[Link]","wb")
f2=open("[Link]","wb")
list1=[]
list_odd=[]
list_even=[]
a=int(input("Enter any ending value:- "))
for i in range(1,a+1):
k=int(input("Enter any value:- "))
[Link](k)
if(k%2==0):
list_even.append(k)
else:
list_odd.append(k)
[Link](list1,f)
[Link](list_odd,f1)
5
[Link](list_even,f2)
f=open("[Link]","rb")
f1=open("[Link]","rb")
f2=open("[Link]","rb")
try:
print("Display..........")
while True:
s=[Link](f)
s1=[Link](f1)
s2=[Link](f2)
print(s)
print(s1)
print(s2)
except EOFError:
[Link]()
Output
Enter any ending value:- 4
Enter any value:- 11
Enter any value:- 12
Enter any value:- 13
Enter any value:- 14
Display..........
[11, 12, 13, 14]
[11, 13]
[12, 14]