The path and simple file reading: Difference between revisions

From 22116
Jump to navigation Jump to search
(Created page with "__NOTOC__ {| width=500 style="float:right; margin-left: 10px; margin-top: -56px;" |Previous: Python Recap |Next: Pseudocode and Comments |} == Required course material for the lesson == Powerpoint: [https://teaching.healthtech.dtu.dk/material/22116/22116_02-PathFile.ppt The path & simple file reading]<br> Resource: Example code - File Reading<br> == Subjects covered == ''with'' statement, just a simple use of it.<br> ''open'' function for opening files for...")
 
mNo edit summary
Line 1: Line 1:
__NOTOC__
__NOTOC__
{| width=500  style="float:right; margin-left: 10px; margin-top: -56px;"
{| width=500  style="float:right; margin-left: 10px; margin-top: -56px;"
|Previous: [[Python Recap]]
|Previous: [[Python recap]]
|Next: [[Pseudocode and Comments]]
|Next: [[Pseudocode and Comments]]
|}
|}
Line 10: Line 10:


== Subjects covered ==
== Subjects covered ==
The file system, folder hierarchies, the path & the library os.path<br>
''with'' statement, just a simple use of it.<br>
''with'' statement, just a simple use of it.<br>
''open'' function for opening files for access. Filehandles.<br>
''open'' and ''close'' function for opening and closing files for access. File handles.<br>
Built-in values; ''True'', ''False'' and ''None''.<br>
Reading and writing files.<br>
Loop control with ''continue'' and ''break'' and how NOT to use them.


== Advice ==
== Advice ==
Line 20: Line 20:


== 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, i.e. the OUTPUT of your print should look precisely like the original file. Hint: look at the spacing between lines.
Again, any use of list is a fail. Why? Because it is overkill and too memory consuming for these kind of tasks. It is also good thinking practice.
# Make the program ask for a filename (the input file in the following exercises), and display the file on the screen.
# Construct a program first asks for a file name and subsequently 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.
# Have you wondered how the '''sum()''' function works? Wonder no longer. 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.
# 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.
# Did you also wonder about the '''max()''' function? Make a program that finds the maximum number in a file. Display the result. This is quite tricky. Hint: '''None''' could be useful. The max in ''ex1_1.dat'' is 10.4155.
# 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.
# 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.<br>For extra difficulty, make the program 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.
# 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. The max in ''ex1_1.dat'' is 10.4155.
# 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.
# 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 ==
== 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 [https://en.wikipedia.org/wiki/Fibonacci_number 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.

Revision as of 17:10, 25 August 2025

Previous: Python recap Next: Pseudocode and Comments

Required course material for the lesson

Powerpoint: The path & simple file reading
Resource: Example code - File Reading


Subjects covered

The file system, folder hierarchies, the path & the library os.path
with statement, just a simple use of it.
open and close function for opening and closing files for access. File handles.
Reading and writing files.

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

Again, any use of list is a fail. Why? Because it is overkill and too memory consuming for these kind of tasks. It is also good thinking practice.

  1. Construct a program first asks for a file name and subsequently counts the number of lines in the input file, and displays the result. Try it with the file ex1.dat. There are 1675 lines.
  2. Have you wondered how the sum() function works? Wonder no longer. 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.
  3. Did you also wonder about the max() function? Make a program that finds the maximum number in a file. Display the result. This is quite tricky. Hint: None could be useful. The max in ex1_1.dat is 10.4155.
  4. 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, make the program 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.

Exercises for extra practice