Python 101 (part 5): Snake Oil For The Soul (
Page 1 of 8 )
Now that you know how to create and use Python's tuples, lists
and dictionaries, it's time to get your hands dirty. In this article, find
out how to read from, and write to, files on the filesystem with built-in
Python methods, and also take a look at some of the new features available
in Python 2.x.
Yeah, yeah, I know. So far, all we've been doing in this tutorial is
mucking around with Python's data structures, dumb creatures with names
like "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 and
wonderfully-elegant though they may be, leave you cold. You just don't see
the point of this exercise. As a matter of fact, you're thinking, now is
probably a good time to drop this thing and get on with something more
worthwhile - "Ally McBeal", perhaps?
I hear you.
Over the next few pages, we're going to leave the rarefied world of Python
data 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 on
your filesystem, opening them, reading them and writing to them.
No longer will you be playing abstract games with list slices and
dictionary keys - by the end of this article, you'll be able to watch as
your Python code creates new files in front of your eyes, or rub your hands
in glee as you read other people's private documents. You will have the
power to wipe out each and every file on your hard drive, or selectively
nuke only the ones you don't like. You will be respected...and, more
importantly, you will be feared.
Even "Ally McBeal" can't top that!