6
Files and the Internet
Python makes it easy for your programs to use files and connect to the Internet. You can read data from files, write data to files, and fetch content from the Internet. You can even check for new mail and tweet—all from your program.
Files
When you run a Python program, any values you have in variables will be lost. Files provide a means of making data more permanent.
Reading Files
Python makes reading the contents of a file extremely easy. As an example, we can convert the Hangman program from Chapter 4 to read the list of words from a file rather than have them fixed in the program.
First of all, start a new file in Mu and put some words in it, one per line. Then save the file with the name hangman_words.txt in the ...
Get Programming the Raspberry Pi, Third Edition: Getting Started with Python, 3rd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.