HomePython Python 101 (part 5): Snake Oil For The Soul
Python 101 (part 5): Snake Oil For The Soul
Now that you know how to create and use Python's tuples, listsand dictionaries, it's time to get your hands dirty. In this article, findout how to read from, and write to, files on the filesystem with built-inPython methods, and also take a look at some of the new features availablein Python 2.x.
Yeah, yeah, I know. So far, all we've been doing in this tutorial ismucking around with Python's data structures, dumb creatures with nameslike "tuples" and "lists", and messing about with twisty things like the"for" and "while" loops. And you're fed up.
All these concepts and structures, beautifully-designed andwonderfully-elegant though they may be, leave you cold. You just don't seethe point of this exercise. As a matter of fact, you're thinking, now isprobably a good time to drop this thing and get on with something moreworthwhile - "Ally McBeal", perhaps?
I hear you.
Over the next few pages, we're going to leave the rarefied world of Pythondata structures and enter something a lot more tangible - the filesystem.I'm going to be showing you how to use Python to interact with files onyour filesystem, opening them, reading them and writing to them.
No longer will you be playing abstract games with list slices anddictionary keys - by the end of this article, you'll be able to watch asyour Python code creates new files in front of your eyes, or rub your handsin glee as you read other people's private documents. You will have thepower to wipe out each and every file on your hard drive, or selectivelynuke only the ones you don't like. You will be respected...and, moreimportantly, you will be feared.