0% found this document useful (0 votes)
35 views

Filing in Python

The document discusses various ways of reading and writing to files in Python. It shows how to open files in different modes like read, write and append. It demonstrates writing strings, integers and other data types to files and reading the same. It also discusses using pickle module to write Python objects to binary files and read them back. The programs cover basic file handling operations like reading a file line by line, random access of file content using positions.

Uploaded by

Srijit Saha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Filing in Python

The document discusses various ways of reading and writing to files in Python. It shows how to open files in different modes like read, write and append. It demonstrates writing strings, integers and other data types to files and reading the same. It also discusses using pickle module to write Python objects to binary files and read them back. The programs cover basic file handling operations like reading a file line by line, random access of file content using positions.

Uploaded by

Srijit Saha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Filing in Python

Program 1

#open txt file in write mode

f1=open("abc.txt","w")

f1.write("Welcome to kolkata")

print("data has been file up......")

f1.close()

Output

data has been file up......

Program 2

#open file in read mode

f1=open("abc.txt","r")

f2=open("text.txt","w")

print(f1.read())#read all data

print(f2.readlines())#read multipal lines

print(f1.read(2))

f1.close()

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("abc.txt","r")

print(f1.read(3))

f1.close()

Output

Wel

Program 4

#open txt file in append mode

f1=open("text.txt","a")

a=int(input("Enter number of string:- "))

for i in range(a):

k=input("Enter your string:- ")

f1.write(k)

f1.write("\n")

f1.close()

Output

Enter number of string:- 2

Enter your string:- welcome

Enter your string:- kolkata

2
Program 5

#open bynary file

import pickle

f=open("number.bin","wb")#binary file open in write mode

num=[5,10,15,20,25]

arr=bytearray(num)#convert array to bytearray

f.write(arr)#data wirite in binary fie

f.close()

f=open("number.bin","rb")#binary file opne in read mode

num=list(f.read())#date read from binary file

print(num)#data display in screen

f.close()

Output

[5, 10, 15, 20, 25]

Program 6

import pickle

f=open("number1.bin","wb")

num=[5,10,15,20,25]

num1=[15,110,151,20,25]

dict={1:"python",2:"C++"}

pickle.dump(num,f)#with help of dump function data write in binary file

pickle.dump(num1,f)

pickle.dump(dict,f)

f.close()

f=open("number1.bin","rb")

try:

3
while True:

s=pickle.load(f)#with help of load function data read from file

print(s)

except EOFError:

f.close()

Output

[5, 10, 15, 20, 25]

[15, 110, 151, 20, 25]

{1: 'python', 2: 'C++'}

Program 7

import pickle

f=open("random.bin","wb")

list1=[]

a=int(input("Enter any ending value:- "))

for i in range(1,a+1):

k=int(input("Enter any value:- "))

list1.append(k)

pickle.dump(list1,f)

f=open("random.bin","rb")

try:

while True:

s=pickle.load(f)

print(s)

except EOFError:

f.close()

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("random.bin","wb")

f1=open("odd.bin","wb")

f2=open("even.bin","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:- "))

list1.append(k)

if(k%2==0):

list_even.append(k)

else:

list_odd.append(k)

p.dump(list1,f)

p.dump(list_odd,f1)

5
p.dump(list_even,f2)

f=open("random.bin","rb")

f1=open("odd.bin","rb")

f2=open("even.bin","rb")

try:

print("Display..........")

while True:

s=p.load(f)

s1=p.load(f1)

s2=p.load(f2)

print(s)

print(s1)

print(s2)

except EOFError:

f.close()

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]

You might also like