Page 66 - Programming the Raspberry Pi Getting Started with Python
P. 66
function rmtree, on the other hand, will recursively remove a directory and all its contents—exercise
extreme caution with this one!
The nicest way of finding out what is in a directory is via globbing. The package glob allows you to
create a list of files in a directory by specifying a wildcard (*). Here’s an example:
If you just want all the files in the folder, you could use this:
Pickling
Pickling involves saving the contents of a variable to a file in such a way that the file can be later
loaded to get the original value back. The most common reason for wanting to do this is to save data
between runs of a program. As an example, we can create a complex list containing another list and
various other data objects and then pickle it into a file called mylist.pickle, like so:
If you find the file and open it in an editor to have a look, you will see something cryptic that looks
like this:
That is to be expected; it is text, but it is not meant to be in human-readable form. To reconstruct a
pickle file into an object, here is what you do:
Internet
Most applications use the Internet in one way or another, even if it is just to check whether a new
version of the application is available to remind the user about. You interact with a web server by
sending HTTP (Hypertext Transfer Protocol) requests to it. The web server then sends a stream of text
back as a response. This text will be HTML (Hypertext Markup Language), the language used to
create web pages.
Try entering the following code into the Python console.