0% found this document useful (0 votes)
13 views6 pages

Obs Prg2024

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views6 pages

Obs Prg2024

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

FILE HANDLING- OBSERVATION PROGRAMS

TEXT FILE
1. Write a method in python to write multiple line of text contents into a text file
mylife.txt.
def writelines():
outfile = open('myfile.txt','w')
while True:
line = input('Enter line: ')
line += '\n'
outfile.write(line)
choice = input('Are there more lines y/n? ')
if choice == 'n':
break
outfile.close()

writelines() # Call the writelines function.

2. Write a function in Python to read lines from a text file diary.txt, and display
only those lines, which are starting with an alphabet 'P'.

If the content of file is:


I hope you will please write to me from all the cities you visit.
Please accept them with the love and good wishes of your friend.
He never thought something so simple could please him so much.

The output should be:


Please accept them with the love and good wishes of your friend.

def readlines():
file = open('diary.txt','r')
for line in file:
if line[0] == 'P':
print(line)
file.close()

readlines() # Call the readlines function.


3. Write a method in Python to read lines from a text file INDIA.TXT, to find and
display the occurrence of the word 'India'. For example, if the content of the file is:

India is the fastest-growing economy. India is looking for more investments around the
globe. The whole world is looking at India as a great market. Most of the Indians can
foresee the heights that India is capable of reaching.
The output should be 4.

def count_word():
file = open('india.txt','r')
count = 0
for line in file:
words = line.split()
for word in words:
if word == 'India':
count += 1
print(count)
file.close()

count_word() # call the function count_word().

4. Assuming that a text file named first.txt contains some text written into it, write
a function that reads the file first.txt and creates a new file named second.txt, to
contain only those words from the file first.txt which start with a lowercase vowel
(i.e. with 'a', 'e', 'i', 'o', 'u').

For example if the file first.txt contains: Carry umbrella and overcoat when it rains
Then the file second.txt shall contain: umbrella and overcoat it

def copy_file():
infile = open('first.txt','r')
outfile = open('second.txt','w')

for line in infile:


words = line.split()
for word in words:
if word[0] in 'aeiou':
outfile.write(word + ' ')
infile.close()
outfile.close()

copy_file() # Call the copy_file function.

5. Write a function in Python to count and display number of vowels in text file.

def count_vowels():
infile = open('first.txt','r')
count = 0
data = infile.read()
for letter in data:
if letter in 'aeiouAEIOU':
count += 1
print('Number of vowels are',count)
infile.close()

count_vowels() # Call the count_vowels function.


BINARY FILE

1. Write a program to write below 2 rows to a binary file “Student.dat”.


[23,’john’,12] and [24,’Tina’,13]

2. Write a program to append below 2 rows to a binary file”student.dat”.


[25,’Banu’,56] and [45,’Geetha’,36]
3. Write a program to write name and rollno into a binary file ‘file.dat’ the data should be in
below format (Name,Rollno)

4. Write a program to read all contents from binary file ‘file.dat’


5. A binary file ‘emp.dat’ has structure [Eid,Ename,Designation,Salary]
i) Write a user defined function CreateEmp() to input data for a record and create a file
emp.dat
ii) Write a function display() in Python to display the detail of all employees whose salary
is more than 50000

You might also like