File Handling Programming Tasks
ONLY upload your HeAD with Code copied as text and Screenshots of console windows with the code running.
There are numerous resources on the internet to help with these file handling exercises, e.g.:
- https://www.w3schools.com/python/python_file_handling.asp
- https://www.tutorialspoint.com/python/python_files_io.htm
Also, videos, e.g.:
Reading from text files of a known length
For the following programs make sure that:
- File is opened
- the contents of the file are assigned to variable, then outputted to the console
- the file is closed
- Copy and paste your code into a head, screen shot of the program running with the file you created also showing
- Create a file that has 5 lines of text on 5 separate lines. Create a program that reads and displays the 5 lines of text
- You should use an array to hold the content of the file
- Create a file that has 5 items of text separated by commas. Create a program that reads and displays the 5 items of text on separate lines in the console
- Make sure your file is comma delimited and the output isn't
- Adding up numbers from a file
- Create a file with 10 numbers (one on each line)
- Write a program that displays the numbers and a final line that clearly displays the sum
- Extra marks for the nicest display of this sum
- No need to use an array for this one
- Adding up numbers from a file
- Create a file with 10 numbers (on single line separated by commas)
- Write a program that displays the numbers and a final line that clearly displays the sum
- Extra marks for the nicest display of this sum
- No need to use an array for this one
Reading text files of an unknown length
6. How many lines?
- Create a file with several lines of text.
- Write a program that outputs the contents of the file and displays all lines, however many there are.
7. Adding up numbers from a file
- Create a file with an unknown number of numbers (one on each line)
- Write a program that displays the numbers and a final line that clearly displays the sum
- Extra marks for the nicest display of this sum
- Put your code and a screen shot of the program running in the HeAD
- NOW try with an unknown number of numbers on a single line separated by commas
Writing to text files
1. Write a single line of text to a file- User inputs a line of text into console
- A pre-prepared file is opened
- the user's input is written to the first line of the file
- File is closed
2. Write a single line of text to the end of a file (filemode=(a)ppend)
- User inputs a line of text into console
- A pre-prepared file is opened
- the user's input is written to a new line at the end of a file
- File is closed
Fun Problems (worth chips!)
10 Find a word (2 chips)
create a program that finds if a word you input is located in a named text file. Extra chip for the line number of the first instance. Extra 2 chips if you output the line number of each instance to the console
11. Find and Replace (4chips)
Create a program that opens a file and replaces every instance of one word with another (e.g. "student" for "chimp"). Create a txt file (just copy a wiki article into a txt file). There are lots of ways to solve this problem. The nicest would be to just change the words in the original file (use read and replace methods). Creating a new file with the altered text is straight-forward.
- open 2 files (1 read, 1 write)
- read each line
- string.replace
- write newline to new file
- close both both files
I will award extra chips for more elegant solutions
12. File Stats.
Create a program that give the statistics for the number of occurrences of each alphabetic characters in a text file (3 chips). An extra 2 chips if you also add a top five words feature. A simple output to the console would be fine. Appending to the bottom of the file would be worth an extra chip.