The path and simple file reading
Jump to navigation
Jump to search
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, which you have put in the same folder as the program. Then you just give the file name and not the full path. 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. BTW, the file name can be the full path or a relative path if the file is not in the same folder as the program.
- 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: The None built-in no-value 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. - Move or copy mixedlines.txt to some other folder for testing with this program. Write a program that asks for just a file name and then adds the full (or relative) path (stored in a variable like a constant) to the file name and then opens file and count the number of empty lines and non-empty lines in the file. Display the two counts.
Warning: Just because you can not see it, it does not mean it does not exists. Invisible characters have long been a curse of programmers. So do not make them yourself. Hint: mixedlines.txt has 19 empty lines and 66 non-empty lines. - Ask for a number - this is your target number. Now ask for a filename - ex1_1.dat, ex1_2.dat & ex1_3.dat are good options. Open the file and compute two sums from the numbers in the file; the sum of the numbers that are higher than your target number and the sum of the numbers that are lower than your target number. Any number in the file that is equal to your target number is ignored. Display the result.
Using the file ex1_1.dat and the target number 0, the 2 sums are; -1523.1255 and 618.7112.