Chapter Six: Files and Exceptions

Introduction to File Input and Output

  • The Volatility of RAM: Programs used previously typically require input from the screen, which must be re-entered every time the program runs because the data is stored in Random Access Memory (RAM). RAM is volatile, meaning output disappears once the shell or program is closed.
  • Non-Volatile Storage: To retain data for later use, it must be written to a non-volatile medium, such as a Hard Drive. This allows the data to be reused by the same program or different programs later.
  • Commercial Applications: Most commercial software packages rely on files for data storage, including:     * Word Processors     * Image Editors     * Spreadsheets     * Games     * Web browsers     * Business applications (e.g., payroll programs)
  • Writing to a File: This is the process of saving data to a file. Data is copied from RAM to the file. The file receiving the data is called an output file.
  • Reading from a File: This is the process of retrieving data from a file. Data is copied from the file into RAM and referenced by a variable. The file being read is called an input file.

The Three-Step Process of File Interaction

  1. Open the File: This creates a connection between the program and the file stored on the disk.
  2. Process the File: Data is either written to the file (output) or read from the file (input).
  3. Close the File: Once the program is finished, the file must be closed to disconnect it from the program and clear any memory buffers.

Types of Files and Access Methods

  • Text Files: These contain data encoded as characters using schemes like ASCII or Unicode. Even numeric data is stored as a series of characters. These files can be viewed and edited in simple text editors like Notepad.
  • Binary Files: These contain data that has not been converted to text. They are intended for program use only and cannot be viewed meaningfully in a text editor.
  • Sequential Access: Data is accessed from the beginning to the end. To read data in the middle, the program must read everything preceding it. This is compared to a VCR tape.
  • Direct Access (Random Access): The program can jump directly to any piece of data in the file without reading the preceding data. This is compared to a CD or MP3 player.

File Management in Python

  • Filenames and Extensions: Operating systems have specific rules for naming files. Extensions (the characters following the period) indicate the file type (e.g., .jpg.jpg is a graphic, .txt.txt is text, .doc.doc is a Word document).
  • File Objects: When a program works with a file, it creates a file object in memory. This object serves as a link between the program variable and the specific file on disk.
  • The Open Function:     * General Format: file_variable=open(filename,mode)file\_variable = \text{open}(filename, mode)     * file_variablefile\_variable: The name of the variable referencing the file object.     * filenamefilename: A string specifying the file name.     * modemode: A string specifying how the file will be used.

Python File Modes

ModeDescription
r'r'Open for reading only. The file cannot be changed.
w'w'Open for writing. If the file exists, its contents are erased. If not, it is created.
a'a'Open for appending. Data is written to the end of the file. If the file doesn't exist, it is created.

Specifying File Locations and Path Rules

  • Current Directory: If no path is provided, Python looks for the file in the same folder as the program.
  • Specified Paths: Paths can be defined literally. Using the rr prefix before the string indicates a raw string, telling the interpreter to treat backslashes as literal characters rather than escape sequences.     * Example: test\_file = \text{open}(r'C:\Users\Home\Other\test.txt', 'w')

Writing and Closing Files

  • Methods: A method is a function belonging to an object. File objects use the .write()\text{.write()} method to perform operations.
  • Closing Files: Essential for disconnecting the program and clearing the memory buffer. Syntax: customer_file.close()customer\_file.\text{close}()
  • Example (file_write.py):     * outfile=open(philosophers.txt,w)outfile = \text{open}('philosophers.txt', 'w')     * outfile.\text{write}('John Locke\n')     * outfile.\text{write}('David Hume\n')     * outfile.\text{write}('Edmund Burke\n')     * outfile.close()outfile.\text{close}()
  • Internal Representation: In the file, the data appears as John Locke\text{\n}David Hume\text{\n}Edmund Burke\text{\n}.

Reading Data from Files

  • The Read Method: infile.read()infile.\text{read}() reads the entire contents of a file into memory as a single string.
  • The Readline Method: infile.readline()infile.\text{readline}() reads a file one line at a time. A line is defined as a string of characters ending with \text{\n}. The method returns the string including the newline character.
  • Read Position: An internal marker maintained by Python that tracks the location of the next item to be read. It starts at the beginning of the file (index 0) and advances forward as data is read.

Formatting and Stripping Newlines

  • Stripping Newlines: The .rstrip()\text{.rstrip()} method is used to remove specific characters from the right side of a string. This is common to remove the \text{\n} character after reading from a file.     * Example: line1 = line1.\text{rstrip}('\text{\n}')
  • Concatenation for Writing: When writing user input to a file, you often must manually concatenate a newline character so each entry appears on a new line.     * Example: myfile.\text{write}(name1 + '\text{\n}')

Writing and Reading Numeric Data

  • String Conversion: Files store data as characters. Therefore, numbers must be converted to strings before writing.     * Example: outfile.\text{write}(\text{str}(num1) + '\text{\n}')
  • Data Retrieval: When reading numbers from a file, they are received as strings and must be converted to numeric types (int\text{int} or float\text{float}) for math operations. Both int()\text{int}() and float()\text{float}() functions ignore the \text{\n} character.     * Example: value=int(infile.readline())value = \text{int}(infile.\text{readline}())

Processing Files with Loops

  • Processing Unknown Amounts of Data: Often the number of items in a file is unknown. In Python, readline\text{readline} returns an empty string ('') when it reaches the end of the file (EOF).
  • The While Loop Algorithm:     1. Open the file.     2. Use readline\text{readline} to read the first line (the priming read).     3. While the line is not an empty string (''):         a. Process the item.         b. Use readline\text{readline} to read the next line.     4. Close the file.
  • The For Loop: A more concise way to read files. The loop automatically stops at the end of the file and does not require a priming read.     * Format: forlineinfile_object:for \, line \, in \, file\_object:

Record Processing and Maintenance

  • Definitions:     * Record: A complete set of data about an item (e.g., an employee's information).     * Field: An individual piece of data within a record (e.g., name, ID number, or department).
  • Midnight Coffee Roasters Case Study:     * Records consist of a Description (String) and Quantity in Pounds (Floating point).     * Modifying Records: To modify a sequential file, you must create a temporary file. Copy all records from the original to the temporary file, replacing the target record with the new data during the copy process. Finally, delete the original and rename the temporary file to the original's name.

Exception Handling

  • Exception: A runtime error that causes a program to crash abruptly.
  • The try/except Statement: Allows for graceful handling of errors.     * The try suite: Contains code that might raise an exception.     * The except clause (handler): Contains code that executes if an exception of a certain type occurs.
  • Specific Exceptions:     * IOErrorIOError: Occurs if a file cannot be opened or found.     * ValueErrorValueError: Occurs if data conversion fails (e.g., trying to convert "abc" to an integer).
  • Handling Multiple Exceptions: Programs can have multiple except clauses to handle different error types separately.
  • Catch-all Except Clause: A bare except:except: clause catches any exception, but it is less precise because it doesn't specify which error occurred.
  • Exception Objects: You can capture the default error message by using exceptExceptionaserr:except \, Exception \, as \, err: and printing errerr.
  • The finally Clause: An optional clause at the end of a try/excepttry/except block that executes regardless of whether an exception occurred. It is used for cleanup tasks like closing files.