0% found this document useful (0 votes)
95 views2 pages

File Handling Assessment Test Solution

The document contains 6 code snippets that demonstrate various ways to work with files in Python. Snippet 1 counts the number of words in a text file. Snippet 2 reverses the contents of a text file. Snippet 3 reads the bytes of a binary file one by one. Snippet 4 copies the contents of one pickle file to another. Snippet 5 displays lines from a text file that have at least 10 words. Snippet 6 counts words ending with a digit in a given text file.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
95 views2 pages

File Handling Assessment Test Solution

The document contains 6 code snippets that demonstrate various ways to work with files in Python. Snippet 1 counts the number of words in a text file. Snippet 2 reverses the contents of a text file. Snippet 3 reads the bytes of a binary file one by one. Snippet 4 copies the contents of one pickle file to another. Snippet 5 displays lines from a text file that have at least 10 words. Snippet 6 counts words ending with a digit in a given text file.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

#1. Write a Python program to count the number of words in a text file.

f = open('article.txt', 'rt')
contents = f.read()
list_of_words = contents.split()
no_of_words = len(list_of_words)
print('Number of words: ', no_of_words)
f.close()

Number of words: 41

#2. Write a Python program to reverse the contents of a text file.


f = open('article.txt', 'r')
contents = f.read()
list_of_words = contents.split()
reversed_list_of_words = reversed(list_of_words)
reversed_content = " ".join(reversed_list_of_words)
print(reversed_content)

f.close()

distance. long see can which eyes, big having was friend other the And distance. long to listen can which earlo
be, big having was friends the of One friends. two were There land away far far a in time a upon Once

#3. Write a Python program to read the contents of a binary file byte by byte.
f = open('random_numbers.bin', 'rb')
all_bytes = f.read()

for a_byte in all_bytes:


print(a_byte, end=" ")
f.close()

17 15 1 15 17 42 2 15 23 5 34 41 5 7 15 27 24 22 32 29 37 19 28 33 20 23 26 22 3 16 24 41 46 15 39 10 29 38 30
28 19 27 45 44 4 26 44 43 10 24 19

#to write random numbers from 1-50 in binary files


import random
f = open('random_numbers.bin', 'wb')
numbers = []
while True:
if len(numbers) <= 50:
numbers.append(random.randint(1,50))
else:
break
f.write(bytes(numbers))
print('Numbers generated and file saved')
f.close()

Numbers generated and file saved

#to write students details into a pickle file


import pickle
students = {
1:{
'name':'Tenzin',
'class':12,
'section':'A',
'marks':98
},
2:{
'name':'Tsering',
'class':12,
'section':'B',
'marks':85
},
3:{
'name':'Kelsang',
'class':12,
'section':'A',
'marks':77
}
}
f = open('students_details.pkl', 'wb')
pickle.dump(students, f)
print('File saved')
f.close()

#4. WAP to copy the contents of one binary file to another binary file using pickle
import pickle
f=open('students_details.pkl', 'rb')
student_data = pickle.load(f)
f.close()

f = open('student_marks.pkl', 'wb')
pickle.dump(student_data, f)
print('File copied')
f.close()

File copied

#5. Write the definition of a Python function named LongLines()


#which reads the contents of a text file named ‘Lines.txt’ and
#display those lines from the file which have at least 10 words in it.

def LongLines():
f=open('Lines.txt', 'r')
all_lines = f.readlines()

for a_line in all_lines:


if len(a_line.split()) >= 10:
print(a_line)

LongLines()

Once upon a time in a far far away land

One of the friends was having big earlobe, which can listen to long distance.

And the other friend was having big eyes, which can see long distance.

'''
Write a function count_Dwords() in Python to count the words
ending with a digit in a text file "Details.txt".
Example:
If the file content is as follows:
On seat2 VIP1 will sit and
On seat1 VVIP2 will be sitting
Output will be:
Number of words ending with a digit are: 4
'''

f = open('Details.txt', 'r')
Digit_ended_word_count = 0

text_file_content = f.read().split()

for a_word in text_file_content:


if a_word[-1].isdigit():
Digit_ended_word_count += 1

print(Digit_ended_word_count)

Loading [MathJax]/jax/output/CommonHTML/fonts/TeX/fontdata.js

You might also like