LPTHW – Lesson 15, 16, 17, 18, 19, 20, 21, 22, 23
These lessons covered a lot of material, so I won’t touch on everything, just some critical points. Lesson 15 actually took me quite a while to finish; I got hung up with an issue when I tried to write to, then read from the same file, like:
new_file = open(filename, ‘r+’)
new_file.write(‘x’)
new_file.read()
new_file.close()
That approach kept adding a bunch of white-space after the ‘x’. I still haven’t figured out exactly why… but it works fine if I just open the file in ‘w’ format, write to it, close it, then re-open it in read format. One thing I have learned about Python is that if you run into an issue like this, there are probably a bunch of different ways to solve the problem. You can always use a workaround and come back to fully understand the issue later.
When reading code, I have occasionally come across *args in function definitions, but never fully understood what it meant. Now I realize it is just a bucket to hold any arguments passed to the function, so they can be unpacked within the function, kind of the same way that argv works – I’m happy to finally (I think) understand that.
Seek also seems like a useful function that probably has many applications beyond just resetting a file to its initial position. I will try to find some more creative uses for it.
Finally, I need to remember to always close() files after opening them. I also need to learn when files are closed automatically, vs when they need to be manually closed.