15.04.2013 Views

Core Python Programming (2nd Edition)

Core Python Programming (2nd Edition)

Core Python Programming (2nd Edition)

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Regardless of your platform, these variables will be set to the correct<br />

values when you import the os module: One less headache to worry<br />

about.<br />

We would also like to remind you that the comma placed at the end of the print statement is to<br />

suppress the NEWLINE character that print normally adds at the end of output. The reason for this is<br />

because every line from the text file already contains a NEWLINE. readline() and readlines() do not<br />

strip off any whitespace characters in your line (see exercises.) If we omitted the comma, then your text<br />

file display would be doublespaced one NEWLINE which is part of the input and another added by the<br />

print statement.<br />

File objects also have a truncate() method, which takes one optional argument, size. If it is given, then<br />

the file will be truncated to, at most, size bytes. If you call TRuncate() without passing in a size, it will<br />

default to the current location in the file. For example, if you just opened the file and call TRuncate(),<br />

your file will be effectively deleted, truncated to zero bytes because upon opening a file, the "read head"<br />

is on byte 0, which is what tell() returns.<br />

Before moving on to the next section, we will show two more examples, the first highlighting output to<br />

files (rather than input), and the second performing both file input and output as well as using the seek<br />

() and tell() methods for file positioning.<br />

filename = raw_input('Enter file name: ')<br />

fobj = open(filename, 'w')<br />

while True:<br />

aLine = raw_input("Enter a line ('.' to quit): ")<br />

if aLine != ".":<br />

fobj.write('%s%s' % (aLine, os.linesep)<br />

else:<br />

break<br />

fobj.close()<br />

Here we ask the user for one line at a time, and send them out to the file. Our call to the write()<br />

method must contain a NEWLINE because raw_input() does not preserve it from the user input. Because<br />

it may not be easy to generate an end-of-file character from the keyboard, the program uses the period<br />

( . ) as its end-of-file character, which, when entered by the user, will terminate input and close the file.<br />

The second example opens a file for read and write, creating the file from scratch (after perhaps<br />

truncating an already existing file). After writing data to the file, we move around within the file using<br />

seek(). We also use the tell() method to show our movement.<br />

>>> f = open('/tmp/x', 'w+')<br />

>>> f.tell()<br />

0<br />

>>> f.write('test line 1\n') # add 12-char string [0-11]<br />

>>> f.tell()<br />

12<br />

>>> f.write('test line 2\n') # add 12-char string [12-23]<br />

>>> f.tell() # tell us current file location (end))<br />

24

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

Saved successfully!

Ooh no, something went wrong!