Simple File Reading: Difference between revisions
Jump to navigation
Jump to search
(Created page with "__NOTOC__ {| width=500 style="float:right; margin-left: 10px; margin-top: -56px;" |Previous: Python Basics |Next: Pseudocode and Comments |} == Required course material for the lesson == Powerpoint: [https://teaching.healthtech.dtu.dk/material/22101/22101_04-FileReading.ppt Simple File Reading]<br> Video: [https://panopto.dtu.dk/Panopto/Pages/Viewer.aspx?id=e51d5f37-84e5-46d4-8252-af270129a697 Simple file reading]<br> Video: [https://panopto.dtu.dk/Panopto/Pages...") |
|||
Line 23: | Line 23: | ||
== Exercises to be handed in == | == Exercises to be handed in == | ||
# Write a program that reads the file ''ex1.acc'' and displays it on the screen. Display it '''correctly''' as it is. | # Write a program that reads the file ''ex1.acc'' and displays it on the screen. Display it '''correctly''' as it is, i.e. the OUTPUT of your print should look precisely like the original file. Hint: look at the lines. | ||
# Make the program ask for a filename (the input file in the following exercises), and display the file on the screen. | # Make the program ask for a filename (the input file in the following exercises), and display the file on the screen. | ||
# Construct a program that counts the number of lines in the input file, and displays the result. Try it with the file ''ex1.dat''. There are 1675 lines. | # Construct a program that counts the number of lines in the input file, and displays the result. Try it with the file ''ex1.dat''. There are 1675 lines. |
Revision as of 08:52, 17 September 2024
Previous: Python Basics | Next: Pseudocode and Comments |
Required course material for the lesson
Powerpoint: Simple File Reading
Video: Simple file reading
Video: Objects, the concept. Some examples
Video: Loop control, pass statement
Video: Boolean Logic, short-circuit evaluation
Resource: Example code - File Reading
Subjects covered
with statement, just a simple use of it.
open function for opening files for access. Filehandles.
Built-in values; True, False and None.
Loop control with continue and break and how NOT to use them.
Advice
You might want to consider what would happen if you feed an empty file to your program.
Are you looking for files? See the top line of the Programme page.
Exercises to be handed in
- Write a program that reads the file ex1.acc and displays it on the screen. Display it correctly as it is, i.e. the OUTPUT of your print should look precisely like the original file. Hint: look at the lines.
- Make the program ask for a filename (the input file in the following exercises), and display the file on the screen.
- Construct a program that counts the number of lines in the input file, and displays the result. Try it with the file ex1.dat. There are 1675 lines.
- Now make a Python program that sums numbers from a input file and displays the sum of the numbers. I have created 3 files with the columns from ex1.dat; ex1_1.dat, ex1_2.dat & ex1_3.dat. Use the 3 files as input and see the sums. The sums are approx. Col 1; -904.4143, Col 2; 482.8410, Col 3; 292.05150 for the three columns.
- Based on the previous program, now make a new one that calculates the mean value of the columns. Create your own test file with some numbers (where you know what the result should be) and feed that to the program.
- Now make a program that counts the number of positive and negative numbers in an input file. Also count the zeroes in the column, if there are any. Display the result.
- Now make a program that finds the maximum number in a column. Display the result. This is quite tricky. Hint: None could be useful.
- Now make a program that finds the minimum number in a column and display the result.
- It begins to get trivial. Now make a program that does all of the above, that is; calculate the sum, the number of lines, the mean value, the number of positive, negative and zero numbers, the maximum and the minimum value. You can only read the file once. Display the all results clearly so there is no doubt about what you display.
Exercises for extra practice
- Ask for some input - it could be a word or a number or a sentence. Then ask for a file name - any file will do, but mixedlines.txt or ex5.acc are good. Now read the file and determine if one (or more) of the lines in the file matches your input precisely. Display "Match" or "No match" accordingly. Hint: A line from a file has a newline at the end, but using the input function does not result in this newline.
- For extra difficulty, do the same program once more, but this time, it should only say "Match" or "No match" once, no matter how many matches there are. Make your own test file, with lots of the same number or word. If you already solved this in above exercise - good for you.
- Ask for 2 integers. Now print the first 20 numbers of the Fibonacci sequence given by those numbers. If the input is 1 & 3, the output would be 1 3 4 7 11 18 29 47 etc.. The next number is the sum of the 2 previous.