File Handling Assessment Test Solution
File Handling Assessment Test Solution
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
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()
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
#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
def LongLines():
f=open('Lines.txt', 'r')
all_lines = f.readlines()
LongLines()
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()
print(Digit_ended_word_count)
Loading [MathJax]/jax/output/CommonHTML/fonts/TeX/fontdata.js