Files and Raw Input

Authors: Tom Dunham
Date: 2009-04-05

String Escapes

The answer is using escape sequences

>>> "he said \"hello\" they said"
'he said "hello" they said'


For a complete list of escape sequences


Characters that don't really print are called whitespace characters.

>>> "he\tsaid hello"
'he\tsaid hello'
>>> print "he\tsaid hello"
he      said hello
>>> print "\n".join("he said hello".split(" "))

String Escapes (2)

So how do you put the character \ into a string?

>>> escape = "\\"
>>> escape
>>> print escape

String Escapes: Reference

Escape Sequence Meaning
newline Ignored
\ Backslash ()
' Single quote (')
" Double quote (")
a ASCII Bell (BEL)
f ASCII Formfeed (FF)
n ASCII Linefeed (LF)
r ASCII Carriage Return (CR)


Escaping the directory separators is important on Windows, or you will not be able to find the file

>>> f = open("barcoding\\barcodingdata.txt", "rb")
>>> f
<open file 'barcoding\\barcodingdata.txt', mode 'rb' at 010E3988>
>>> f.mode

Reading Files

Read the whole file with read

>>> f = open("barcoding\\barcodingdata.txt")
>>> f
<open file 'barcoding\barcodingdata.txt', mode 'r' at 0x00E2AE78>
>>> data =
>>> len(data)
>>> f.close()

Reading chunks

If you have a large file it is often more efficient to read and process it in chunks.

Often, you will want to read every line in a file

for line in open("barcoding\\barcodingdata.txt"):
    print line

Breaking a loop

If you are processing a large file, you may find what you are interested in quickly and want to stop processing. You can jump out of a loop using break:

for line in open("barcoding\\barcodingdata.txt"):
    if len(line) > 100:
    print line

Writing to a file

Open the file for writing:

>>> outf = open("foo.out", "w")
>>> outf
<open file 'foo.out', mode 'w' at 0x01118650>
>>> outf.write("hello world")
>>> outf.close()

Closing files

A file object (as returned by open) is a new kind of object - it refers to an external resource.

Files are a resource that belongs to the operating system. When you open them, you gain a way to manipulate the file, it's important to show you are finished with them by calling close.

If your script finishes, Python closes all external resources for you.

Raw Input

You can prompt the user with raw_input.

>>> colour = raw_input("what is your favorite colour? ")
what is your favorite colour? Blue
>>> colour


See handout

1. On the first day, our first program printed "Hello world". Write a program that asks the user for their name, and greets them by name.

2. The file crosswd.txt is a list of words, one word on every line of the file. Read the file in, split it into a list of words and find out how many words there are.

3. You could also count the number of words by reading the file line-by-line and counting the number of lines you have read. Try this, and compare your result to your first answer.