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