In my previous life*, if you couldn't access data, you couldn't do nothing. In this life I figure it's pretty much the same thing. I really want to get to the point of opening and closing - and reading and writing files - any files. After I can do something, then I'll worry about how to do my thing.
I actually found that if I skipped to files in the Python Tutorials, I could understand the section and was able to use the interactive command line to test my understanding. Because I type badly, and because the error reports make typos sound like calamities instead of just stupid, I first tried to assign a file name to a variable as a text string. That was the subject of my previous entry. Then I tried the open statement (which actually creates a file object, but more later). Then, still using the tutorial as my guide, I read a little sample text file line by line and displayed each line to the screen. Now that's doing something! Well, at least it's like doing a little something.
I need to say that I made a subdirectory below Python31 to store my tiny little tweaks.
Anyway, my efforts were some thing like this:
First I created a text file called testfile.txt. It containes these three lines:
this is the first line of the file.
this is the second line of the file.
this is the line at the end of the file.
Then I tried to read the file:
>>>fn = "c:/python31/sandbox/testfile.txt
>>> fi = open(fn)
>>> for line in fi:
... print(line, end=' ')
Hey! I got the lines from my textfile on the screen. I've got to learn more about the print statement, for all I know, may-be you can produce a really complicated report with a few variations. I've got to learn a bit more about the for statement, it looks different in Python. And either lines is a key word with special powers or there are wonders yet untold hidden in the funny looks of for ___ in ___. But for now, I moved text from the disk to the screen with Python.
Now I want to do it with a script.
* OK, it wasn't a previous life; I'm not Shirley MacLaine. It was, however, several generations ago in Information Technology years. At that time the driving questions seemed to be, "What are we working with?", "Where do we keep it?" and "How do we show that the work is done?" After I'd bragged about the clever algorithms and slick performance optimizations, someone (usually the boss) would point at the data and demand a report.