Simple File Reading

From 22101
Revision as of 10:05, 30 October 2024 by WikiSysop (talk | contribs) (→‎Exercises to be handed in)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
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

  1. 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 spacing between lines.
  2. Make the program ask for a filename (the input file in the following exercises), and display the file on the screen.
  3. 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.
  4. Now make a Python program that sums numbers from ONE input file and displays the sum of the numbers. I have created 3 input files with the columns from ex1.dat; ex1_1.dat, ex1_2.dat & ex1_3.dat. Use the 3 files as input ONE AT A TIME and see the sums. The sums are approx. Col 1; -904.4143, Col 2; 482.8410, Col 3; 292.05150 for the three columns.
  5. 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.
  6. 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.
  7. Now make a program that finds the maximum number in a column. Display the result. This is quite tricky. Hint: None could be useful. The max in ex1_1.dat is 10.4155.
  8. Now make a program that finds the minimum number in a column and display the result. The max in ex1_3.dat is -4.3967. Hint: What happens if all the numbers in the file are positive.
  9. 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.