Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Accelerated Programming 2 Part I: Python Programming 11 Files and Modules © 2010 David A Watt, University of Glasgow 1 Files A file is a collection of data held on a non-volatile memory device (such as a disk). A text file is a file that contains characters grouped into lines. – Each line is terminated by a newline character ‘\n’. 11-2 Text files: opening and closing To open a text file named xyz.txt (i.e., make it available for reading and/or writing): f = open('xyz.txt', 'r') opens the file for reading only f = open('xyz.txt', 'w') opens the file for reading and writing To close file f (i.e., make it unavailable for reading or writing): f.close() 11-3 Text files: writing To write to file f: f.write(s) appends the characters of string s to the file Remember to write ‘\n’ at the end of every line. 11-4 Text files: reading To read from file f: f.read() reads all characters from the file, and returns them as a string. f.readline() reads characters from the file, up to and including ‘\n’, and returns them as a string. f.readlines() reads all characters from the file, and returns them as a list of strings (one string per line). If all characters have already been read from file f, these functions return an empty string or empty list. 11-5 Example: processing a text file Function to filter lines starting with ‘#’ from a text file: def filter (inname, outname): # Copy the file named inname into the file named # outname, except for any lines starting with ‘#’. inf = open(inname, 'r') outf = open(outname, 'w') line = inf.readline(); while line != '': if line[0] != '#': outf.write(line) line = inf.readline() inf.close() outf.close() 11-6 Modules A module is a collection of Python code held in a single file. The code typically consists of: – assignment-statements (defining variables) – function definitions – definitions of other entities not covered in this course. The module is said to export the variables, functions, etc., defined in this way. A module named M must be held in a file named M.py. 11-7 Module import A simple program consists of a single module, but a large program consists of many modules. Suppose that module A needs to use the variables, functions, etc., exported by module B. Then A must include an import-statement: import B Now A can refer to B’s exported variables, functions, etc., using dot notation: B.v accesses a variable v exported by module B B.f(…) calls a function f exported by module B 11-8 Example: trigonometric module (1) The following module exports variable pi and functions sin and cos: # trig.py pi = 3.1416 def sin (x): # Return the sine of x radians. … def cos (x): # Return the cosine of x radians. … 11-9 Example: trigonometric module (2) The following program uses the trigonometric module: import trig makes pi, sin, cos, etc., accessible using dot notation # Tabulate the sine and cosine of 0, 1, 2, ..., 359 degrees. print 'x', '\t', 'sin x', '\t', 'cos x' for x_degree in range(0, 360): x_radian = x_degree / 180 \ imported * trig.pi variable print x_degree, \ '\t', trig.sin(x_radian), \ imported '\t', trig.cos(x_radian) functions 11-10 Example: trigonometric module (3) More concisely: from trig import * makes pi, sin, etc., accessible directly # Tabulate the sine and cosine of 0, 1, 2, ..., 359 degrees. print 'x', '\t', 'sin x', '\t', 'cos x' for x_degree in range(0, 360): x_radian = x_degree / 180 * pi print x_degree, \ '\t', sin(x_radian), \ '\t', cos(x_radian) 11-11 Example: spell-checker (1) A spell-checker is an application that highlights (possibly) mis-spelled words in a given document. A spell-checker uses a word-list. If a word is not in the word-list, it is assumed to be mis-spelled. Since no word-list is comprehensive, many spellcheckers enable the user to add new words to it. 11-12 Example: spell-checker (2) Let’s make the word-list a module: # wordlist.py words = ['a', 'an', 'and', 'be', 'bear', \ 'die', 'live', 'not', 'of', \ 'or', 'that', 'the', 'to'] def contains (word): # Return True iff word is in the word-list. return (word in words) def remember (word): # Add word to the word-list. global words if word not in words: words += [word] This statement is needed to ensure that words is treated as a global variable (not a local variable). 11-13 Example: spell-checker (3) Why bother making the word-list a module? We might want to modify it later to make it more efficient, e.g.: – modify remember to keep the word-list sorted – modify contains to use a more efficient searching algorithm. We don’t want such modifications to affect the spell-checker proper. 11-14 Example: spell-checker (4) Now here is a first version of the spell-checker: import wordlist imports the word-list def check (docname): doc = open(docname, 'r') line = doc.readline() imported while line != '': function for word in all_words(line): if not wordlist.contains(word): print word temporary line = doc.readline() doc.close() def all_words (line): # Return a list of all the words in line. … 11-15 Example: spell-checker (5) The first version simply prints out (possibly) misspelled words. The second version asks the user to decide whether a word is really mis-spelled: def query (word): response = raw_input('Is "' + word + \ '" really mis-spelled?') if response == 'no': wordlist.remember(word) Now replace the temporary code “print word” by “query(word)”. 11-16 Example: spell-checker (6) Let’s now implement the all_words function: def all_words (line): ws = [] i = 0 while i < len(line): if line[i].isalpha(): start = i i += 1 while line[i].isalpha(): i += 1 word = line[start:i] ws += [word] temporary else: i += 1 return ws 11-17 Example: spell-checker (7) Unfortunately this program is case-sensitive (e.g., it treats ‘this’ and ‘This’ as different words). The easiest solution to this problem is: – ensure that the word-list contains only lower-case words – convert every word in the document to lower-case. Replace the temporary code in the all_words function by: words += [word.lower()] 11-18 Library modules Python has a large library of useful modules. For comprehensive documentation see: docs.python.org/modindex.html 11-19 Conclusion Python is a very nice language for writing scripts and general-purpose applications. It provides excellent support for processing of strings, tuples, lists, and dictionaries. It is a very dynamic language. This is both a strength and a weakness: – The language is very flexible. – Types are repeatedly checked at run-time (slow). – Programs are interpreted (slow). – For systems programming, better stick to C! 11-20