0% found this document useful (0 votes)
53 views4 pages

File Handling and Word Count Exercises

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)
53 views4 pages

File Handling and Word Count Exercises

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

File Handling

Lưu ý: Để tránh lỗi không đọc được tiếng Việt, nội dung các file .txt trong toàn bộ bài tập này SV nên
ghõ tiếng Việt không có dấu.

Questions 1 (2 marks) Reimplement the programs in Test 10 (Files and Modules)

Questions 2 (3 marks) Write a program to count the number of words in a text file

Questions 3 (3 marks) Write a program to copy the content of one file to another text file

Given:

Expected: 16

Hint: you may choose either Approach 1 or Approach 2 for this exercise
Approach 1: Apply loop method
§ Use the keyword ‘with’ to open the file1.txt with read mode.
§ Use the keyword ‘with’ to open the file2.txt with write mode.
§ Read line by line from the file1.txt and write it to file2.txt.
§ Sample code (you may modify for your best convinience).
Approach 2: Apply file method
§ Open or create the file2.txt as the output file with write mode.
§ Open the file1.txt as the input file with read mode
§ Read line by line from the input file and write that line to the output file.
§ Close the output file.
§ Sample code (you may modify for your best convinience).
Questions 4 (5 marks) Write a program to to find the word that most frequent in a text file
Given: filename = ‘twitter_user_preprocessed.txt’

Expected output:
Most repeated word: pet
Frequency: 4

Hint: the program should be organized into functions


You need to implement two functions:

1. word_statistic (3 marks)

2. find_max_frequency (2 marks)

You might also like