16.01.2014 Views

Beginning Python - From Novice to Professional

Beginning Python - From Novice to Professional

Beginning Python - From Novice to Professional

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

264 CHAPTER 11 ■ FILES AND STUFF<br />

More useful implementations could do such things as s<strong>to</strong>ring data in a data structure,<br />

computing a sum, replacing patterns with the re module, or perhaps adding line numbers.<br />

Also, <strong>to</strong> try out the examples, you should set the variable filename <strong>to</strong> the name of some<br />

actual file.<br />

Doing It Byte by Byte<br />

One of the most basic (but probably least common) ways of iterating over file contents is <strong>to</strong> use<br />

the read method in a while loop. For example, you might want <strong>to</strong> loop over every character<br />

(byte) in the file. You could do that as shown in Listing 11-6.<br />

Listing 11-6. Looping Over Characters with read<br />

f = open(filename)<br />

char = f.read(1)<br />

while char:<br />

process(char)<br />

char = f.read(1)<br />

This program works because when you have reached the end of the file, the read method<br />

returns an empty string, but until then, the string always contains one character (and thus has<br />

the Boolean value true). So as long as char is true, you know that you aren’t finished yet.<br />

As you can see, I have repeated the assignment char = f.read(1), and code repetition is<br />

generally considered a bad thing. (Laziness is a virtue, remember?) To avoid that, I can use the<br />

while True/break technique you first encountered in Chapter 5. The resulting code is shown in<br />

Listing 11-7.<br />

Listing 11-7. Writing the Loop Differently<br />

f = open(filename)<br />

while True:<br />

char = f.read(1)<br />

if not char: break<br />

process(char)<br />

As mentioned in Chapter 5, you shouldn’t use the break statement <strong>to</strong>o often (because it<br />

tends <strong>to</strong> make the code more difficult <strong>to</strong> follow); even so, the approach shown in Listing 11-7 is<br />

usually preferred <strong>to</strong> that in Listing 11-6, precisely because you avoid duplicated code.<br />

One Line at a Time<br />

When dealing with text files, you are often interested in iterating over the lines in the file, not<br />

each individual character. You can do this easily in the same way as we did with characters,<br />

using the readline method (described earlier, in the section “Reading and Writing Lines”), as<br />

shown in Listing 11-8.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!