Simple Text Files

General Trackbacks (0) Add comment   

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.

Open(), Escape And Windows

Python 3.1 Trackbacks (0) Add comment   

I have discovered that thinking like other people is hard to do. I have ''looked at (as opposed to - 'have read') several introductory Python books. I have even (gasp) spent money on a few. Not only do they seem to work hard at helping the reader understand how to write code "Pythonicly", they each approach the reader differently. For a while I was looking for the author who would explain things in a way that made sense to me; I finally decided to use my books and the included Python documentation as references while I tried to learn the language my way.

The first thing I thought I would do was access an existing text file. I mean, I'm a Python Newbie, not a total coding rookie; I have opened and closed files before - how hard could it be? Then I spent way too long typing variations of open("c:\folder\subfolder\fname.txt"). That example probably tells you that I'm not too proud to do Windows. I figured out fairly soon that the system was putting a second backslash in front of folder and in front of subfolder. It took me a lot longer to realize that the problem was that Python was not putting two backslashes in front of the filename. I'm really indebted to Mark Lutz and chapter 3 of his book Programming Python for the insight. He explained the problem and the solution - in a little box that was titled "Windows Directory Paths". I learned that there was more than one workable solution after I understood that the backslash meant Esacpe to my computer. The single backslash followed by an 'f' was interpreted as a form feed and therefor the open function wasn't working.

So, type two backslashes where ever Windows expects one. Or, an 'R' or 'r' (stands for raw) immediately before the string (as in r"c:\folder\subfolder\filename" or best of all, enter the string with front slashes so the application has a better chance of working on Linix systems as well!

 

Getting Started

General Trackbacks (0) Add comment   

Hey, Look! There's a place to find people talking about Python! At least that's what I thought when I saw PythonBlogs.Com  It doesn't seem to be a real snake pit of activity - which is good for me because I'm not real good with snakes - I'm sure not real good with Python.

 After noticing that Python is at version 3.1 and the books that I bought are around version 2.5, I am going to attempt learning this language primarily by using the interactive interpreter to test the principals introduced in the books and correcting for the variations in syntax (and their underlying shifted meanings.) I figure that will lead to a lot of false starts and do overs. That, in my mind, justifies selecting "Ministry of Silly Walks" as the title of this blog. Now, as I pursue the Grail of understanding, if there are any comments, I may have to change the name to "The Argument Sketch".If there are no comments, then I'll simply be left with a notebook recording the gyrations I've gone through trying to get this right.

Design by N.Design Studio
Powered by Lifetype. Template adapted by Russian Lifetype