background preloader

Basics

Facebook Twitter

Exercise 16: Reading And Writing Files — Learn Python The Hard Way, 2nd Edition. If you did the Study Drills from the last exercise you should have seen all sorts of commands (methods/functions) you can give to files.

Exercise 16: Reading And Writing Files — Learn Python The Hard Way, 2nd Edition

Here's the list of commands I want you to remember: close -- Closes the file. Like File->Save.. in your editor.read -- Reads the contents of the file. You can assign the result to a variable.readline -- Reads just one line of a text file.truncate -- Empties the file. Watch out if you care about the file.write('stuff') -- Writes "stuff" to the file. For now these are the important commands you need to know. Let's use some of this to make a simple little text editor: That's a large file, probably the largest you have typed in. There are actually two things you will see. . $ python ex16.py test.txt We're going to erase 'test.txt'.If you don't want that, hit CTRL-C (^C).If you do want that, hit RETURN.? Now, open up the file you made (in my case test.txt) in your editor and check it out. Is the truncate() necessary with the 'w' parameter?

See Study Drills 5. Loop through files in directory and modifying them on the fly. 7. Input and Output. There are several ways to present the output of a program; data can be printed in a human-readable form, or written to a file for future use.

7. Input and Output

This chapter will discuss some of the possibilities. 7.1. Fancier Output Formatting So far we’ve encountered two ways of writing values: expression statements and the print statement. (A third way is using the write() method of file objects; the standard output file can be referenced as sys.stdout. Often you’ll want more control over the formatting of your output than simply printing space-separated values. The string module contains a Template class which offers yet another way to substitute values into strings. One question remains, of course: how do you convert values to strings? The str() function is meant to return representations of values which are fairly human-readable, while repr() is meant to generate representations which can be read by the interpreter (or will force a SyntaxError if there is no equivalent syntax).

Some examples: Exercise 3: Numbers And Math — Learn Python The Hard Way, 2nd Edition. Every programming language has some kind of way of doing numbers and math.

Exercise 3: Numbers And Math — Learn Python The Hard Way, 2nd Edition

Do not worry: programmers lie frequently about being math geniuses when they really aren't. If they were math geniuses, they would be doing math, not writing ads and social network games to steal people's money. This exercise has lots of math symbols. Let's name them right away so you know what they are called. As you type this one in, say the names. . + plus- minus/ slash* asterisk% percent< less-than> greater-than<= less-than-equal>= greater-than-equal Notice how the operations are missing? $ python ex3.py I will now count my chickens:Hens 30Roosters 97Now I will count the eggs:7Is it true that 3 + 2 < 5 - 7?

Above each line, use the # to write a comment to yourself explaining what the line does.Remember in Exercise 0 when you started Python? Why is the % character a "modulus" and not a "percent"? Mostly that's just how the designers chose to use that symbol. How does % work? What is the order of operations?