HomePython Page 6 - Python 101 (part 5): Snake Oil For The Soul
Desperately Seeking Python - Python
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.
There are a few other miscellaneous methods you should know about.
The seek() method moves the internal file pointer to a specific bytelocation, while the tell() method returns the current location of the filepointer.
>>> data = open("snakeoil.txt")
'rch, scientists at AB Labs have come up with a radical new product, once
that promises to improve the quality of life for humans and animals
The truncate() method is used to truncate a file to a specific byte size.
>>> data = open("snakeoil.txt", "a")
And there are also a couple of interesting file properties you might liketo use in your Python programs. The "mode" and "name" properties return thefile mode and file name respectively, while the "closed" property returns aBoolean value indicating whether or not the file is closed.
Python also comes with an "os" module, which adds a bunch of other filemethods and properties to the built-in ones described above...but I'llleave that to you to experiment with. In case you don't know what a moduleis, don't worry - all will be explained soon!