Text File Handling Assignment (Solved)

Text File Handling Assignment – The aim of this solved assignment is to give the students the basic understanding of the Text File handling in Python. Before anything else, Let me explain three main point

  1. How Python Open a File
  2. Reading of a Text File in Python
  3. Closing of a Text File

1.  Opening a Text File :

A file in Python can be opened in different ways but the most simple method is using the Open( ) function. Open( ) function accept two-parameter a) Name of the file and b) Mode.

Syntaxt : 

file = open(‘file name’, mode)

When we do not pass any mode while opening a file, Python automatically opens the file in reading mode.  You can read more about python file mode in this Article.

2.  Reading of a Text File

Once file is opened, It is ready to read/ write the data. We have three reding methods available in Python that is responsible for reading data from a text file

  1. read( )  method- Read the whole text file in a single go and return the data in a string format. If any integer parameter is passed in read method then this function read that much of character in a single go.
  2. readline ( ) method – This method read only a single line at a time and return this data in string format.
  3. readlines( ) method – Read the whole text file in a single go and return the data in a format of list of strings 

3. Closing of a Text File

An opend Text File can be closed using close() method. The syntax and example of close method is as follows

syntax 

file_object.close( )
Example 
file = open('abcd.txt')
........
.....
file.close()

Hope this basic understanding will help you to understand the below-solved assignment on Text File handling in Python.

Q1. Write a program in Python to read a Text File ‘abcd.txt’

file = open('abcd.txt','r')
data = file.read()
print(data)

Q2. Write a program in python to read first 5 char of a text file ‘abcd.txt’

file = open('abcd.txt','r')
data = file.read(5)
print(data)

Q3. Write a program in python to read first line of a Text File’abcd.txt’

file = open('abcd.txt','r')
data = file.readline()
print(data)

Q4. Write a program in python to find out total number of lines in a Text File ‘abcd.txt’

file = open('abcd.txt','r')
data = file.readlines()
print(len(data))

Q5. Write a program in python to print the first line of the text file using readlines() function (‘abcd.txt’)

file = open('abcd.txt','r')
data = file.readlines()
print(data[0])

Q6.Write a python program to display the first character of all the lines from a Text File ‘abcd.txt’

file = open('abcd.txt','r')
data = file.readlines()
for row in data:
    print(row[0])

Q7Write a program in python that display first char of all the lines in uppercase from the text file ‘abcd.txt’

file = open('abcd.txt','r')
data = file.readlines()
for row in data:
   print(row[0].upper()+row[1:])

Q8. Write a program in python to find out total number of chars in a text file ‘abcd.txt’

file = open('abcd.txt','r')
data = file.read()
print(len(data))

NOTE: The above program will also count EOF ( end of file counter) as a character thus the actual char size is different from the result shown by the program

Q9.Write a program in python to find out total number of characters available in the first line of the text file ‘abcd.txt’

file = open('abcd.txt','r')
data = file.readline()
print(len(data))

Q10. Write a program in python to find out total number of characters available in the first line of text file ‘abcd.txt’ file using readlines() function.  ( Do it yourself )

 

Q11. Write a python program to display last three characters of all the lines available in the text File ‘abcd.txt’

file = open('abcd.txt','r')
data = file.readlines()
for row in data:
   print(row[-3:])

Q12. Write a program in python to display the content of a text file ‘abcd.txt’ file in uppercase.

file = open('abcd.txt','r')
data = file.read()
print(data.upper())

Q13. Write a program in python to find out all the uppercase characters available in text file ‘abcd.txt’

file = open('abcd.txt','r')
data = file.read()
count=0
for x in data:
   if x.isupper():
      count+=1
print('Total Uppercase characters :',count)

Q14. Write a program in Python to count total number of vowels available in a text File ‘abcd.txt’

file = open('abcd.txt','r')
data = file.read()
count=0
for x in data:
   if x in 'aAeEiIoOuU':
     count+=1
print('Total Vowels :',count)

Q15. Write a program in Python to write the following sentences in a text file ‘data.txt’
Hello Dear, I am Learning Python
I am class 12 student in DAV School, Chander Nagar

file = open('data.txt','w')
file.write('Hello Dear, I am Learning Python\n')
file.write('I am class 12 student in DAV School, Chander Nagar')
file.close()

Q16. Write a program in python to read the content of a text file ‘abcd.txt’ file and copy the same content in another file ‘Copy.txt’ file

file1 =open('abcd.txt','r')
file2 =open('copy.txt','w')
data = file1.read()
file2.write(data)
file1.close()
file2.close()

Q17. Write a program in Python to read a word from keyboard and find out the frequency of this word in a text file ‘abcd.txt’

file =open('abcd.txt','r')
word = input('Enter a word :')
data = file.read()
print('Frequency of ',word, 'is :', data.count(word))

Q18. Write a program in python that read a text file ‘abcd.txt’ and copy all those lines that start with ‘the’ into another text file ‘copy.txt’

file =open('abcd.txt','r')
file1= open('copy.txt','w')
data = file.readlines()
for row in data:
   if row[:3]=='the':
       file1.write(row)
file.close()
file1.close()

Q19. Write a python program to read the text file ‘abcd.txt’ and replace the word ‘This’ with ‘That’ in this file.

file = open(abcd.txt', 'r')
data = file.read()
print(data.replace('this','that'))
file.close()

Q20. Write a python program to read the text file ‘abcd.txt’ and display the content after removing the word ‘This’

file = open(abcd.txt', 'r')
data = file.read()
print(data.replace('this',''))
file.close()

Q21. Write a program in python to create a user defined list of 5 numbers( input number from keyboard ) write this list of numbers in a text file ‘data.txt’

file = open(data.txt', 'w')
list1=[]
for x in range(5):
   list1.append(input("Enter any number "))
file.writelines(list1)
file.close()

Hope this Solved Text File handling Assignment on Python will help to understand the basic working of Python, and how python handling the Text file while reading the data.

Related Posts

If you like CBSEToaday and would like to contribute, you can also write an article using submit article or mail your article to contribute@cbsetoday.com See your article appearing on the cbsetoday.com main page and help other students/teachers.