CS108 Lecture 09: Computing with Text Reading and writing files Aaron Stevens 6 February 2009 1 Overview/Questions – Review: string operators and operations Additional examples, if needed – How else can we manipulate text? – How to we read/store text/data on disk? 2 1
Files and File Processing Data stored in secondary storage on disk is organized in files . Why use files? – Storage of data when computer is off – Batch input: processing large amounts of data without typing input at the keyboard – Batch output: processing large amounts of data without needing to read it all from the screen 3 Backgrounder: Files and Directories Pictured: Windows Explorer (left) and Finder (right), showing directories and files stored on my computer. 4 2
Backgrounder: Files and Directories File A named collection of related data, used for organizing secondary storage (e.g. on disk) Directory (a.k.a. Folder) A named group of files. Path (a.k.a. filename or directory) A logical name used to identify a location in a file system. 5 Directory Trees Recall that a directory is a named group of files. A directory can also have subdirectories. It’s easy to visualize this as a hierarchical tree: Directory / Directory text … Files jane.txt alcohol.txt 6 3
Directory Trees Directory tree A logical view of the nested directory organization of a file system. Each directory is a branch, and each file is a leaf. Root directory The directory at the highest level. Every directory descends from the root. Parent directory Every directory has a parent, up to the root. Subdirectory A directory contained within another directory. 7 A Windows Directory Tree 8 An Example of the Windows directory tree. 4
A Unix (Mac) Directory Tree 9 An Example of a UNIX directory tree; Mac OS is based on BSD UNIX. Path Names Absolute Path A path that begins at the root and includes all successive subdirectories. Examples: C:\text\alcohol.txt (Windows) /Users/azs/alcohol.txt (Mac) /home/fac3/azs/alcohol.txt (Linux) 10 5
Path Names Present Working Directory The default/current path used by a program or user. Relative Path A path name that begins at the present working directory. Example: If present working directory is /home/fac3/azs Then the relative path alcohol.txt is sufficient. 11 File Types File Type Describes the kind of information contained in a file. File extension Part of a file name that indicates the type File names are often in two parts: <filename>.<extension> 12 6
File Extensions Some common file types and their extensions. Microsoft pioneered the 8.3 filename standard. Why give file names different extensions? What is the benefit to the user? For more info, see http://filext.com/faq/file_extension_information.php 13 Text and Binary Files Text file A file that contains characters from the ASCII or Unicode character sets. Binary file A file that contains data in a specific (non- text) format, requiring special interpretation interpretation of its bit pattern. 14 7
Text and Binary Files The terms text file and binary file are somewhat misleading. Ultimately, all information on a computer is stored as binary digits. Text files are formatted as chunks of 8 or 16 bits, interpreted as characters. Binary files are formatted in some other special format. 15 Files: Mechanics To use a file in a program we need: – A way to associate the file with a variable in the program (called a file handle ) – A set of operations which manipulate the file (e.g. reading and writing) – A way to close the file when we are done using it. It is crucial to specify a valid filename. 16 8
Opening a File General form for opening a file: <filevar> = open(<filename>,<mode>) – <filename> is either a relative path or absolute path to a file on disk. – <mode> flag tells the operating system how to use the file. Valid choices are “r”, “w”, “a” Default action is “r” 17 File Operations A <filevar> has the following operations: –<filevar>.read() –<filevar>.readline() –<filevar>.readlines() –<filevar>.write() –<filevar>.close() 18 9
Reading from a File General form for opening a file: <filevar> = open(<filename>,<mode>) A <filevar> can be treated as a sequence, and iterated with a for loop: Example: >>> f = open("c:/text/alcohol.txt") >>> for line in f: ... print line >>> f.close() 19 Writing to a File Open the file with the mode of “w” for writing. Example: >>> f = open("c:/text/output.txt", "w") >>> f.write("Some text to the file\n") >>> f.write("Some more text to the file\n") >>> f.write("Enough already!\n") >>> f.close() 20 10
Take-Away Points – Files and directories – File types and encoding – Reading files, writing files 21 Student To Dos – HW04: due 2/10 – Readings: Reading: chapter 4 (this week) Reading: chapter 5 (next week) – Next week, we will use some graphics examples to motivate a discussion of classes, objects, and methods. Download the graphics.py module from http://mcsp.wartburg.edu/zelle/python/graphics.py, and save it in your code folder (with your examples). 22 11
Recommend
More recommend