How to get python to read files for the flashcards from Monday?
We'll go over this next time; we will read the input from a file and parse it into different pieces of information.
How do we get Python to recognize when a part of a line in a file is a certain type, and get it to split the line there? Eg. go through a file looking for all the s, so we can append that into a list.
We'll practice this a lot in future exercises and on labs, but the string method .split()
is very useful.
What's a file, really? Like on a physical level is it written down somewhere?
Yes. We've been dealing with the computer at a level of abstraction this semester, but yes, the computer is a physical object, and everything that you see on the screen is the result of physical manipulations of actual stuff. A file is, on a physical level, written down somewhere --- usually on the hard disk. For LOTS more details about how this works, I recommend you take CS 31!
How do you more efficiently call a file instead of going line by line?
That's it. You just have to read the file line by line. It actually is pretty quick, because computers are fast at reading text.
How to create files
We haven't done any examples, but if you open a file for writing by doing open('filename','w')
, then you can use Python to write to the file. Later, if you close and reopen the file for reading, whatever you wrote there will still be there, in the order you wrote it.