The path and 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 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 | |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. | ''open'' and ''close'' function for opening and closing files for access. File handles.<br> | ||
Reading and writing files.<br> | |||
== Advice == | == Advice == | ||
Line 20: | Line 20: | ||
== Exercises to be handed in == | == 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. | |||
# | # 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. | ||
# 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. | |||
# | # 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. | ||
# | # 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. | ||
# | |||
== Exercises for extra practice == | == Exercises for extra practice == | ||
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.
- 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.
- 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.
- 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.
- 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.