Chapter 7: Files in Python Programming

Overview of File Processing in Python

  • Need for File Analysis: Frequently, data analysis requires the examination of text files that are stored in secondary memory.
  • Text File Definition: A text file is a sequence of lines, which can be contrasted with a string that represents a sequence of characters.

Newline Character

  • End of Line Marker: The newline character (\n) indicates the end of a line in text files. It is treated as a single character.
    • Example:
      python stuff = 'Hello\nWorld!' print(stuff)
    • Output:
      Hello World!
    • The length of stuff including newline character:
      python len(stuff) # Returns 3

Opening Files in Python

  • Using open() Function:
    • To manipulate a file, use the open() function:
      handle = open(filename, mode)
    • Example: fhandle = open('mbox.txt', 'r') returns a handle for read operations.
    • Mode Options:
    • 'r': Read only.
    • 'w': Read and write; creates a file if it doesn't exist.

File Handle Characteristics

  • File Handle as a Sequence: The file handle, when opened for reading, acts as a sequence of strings, with each line representing a string.
    • Example:
      python xfile = open('mbox.txt') for cheese in xfile: print(cheese)

Error Handling

  • File Open Errors: Occurs if a file does not exist or is located in a different directory. Example of error:
  fhand = open('stuff.txt')
  • Output:
    FileNotFoundError: [Errno 2] No such file or directory: 'stuff.txt'

Reading and Counting Lines

  • Counting Lines in a File:
    • Open a file and iterate through each line to count:
      python fhand = open('mbox.txt') count = 0 for line in fhand: count += 1 # or count = count + 1 print('Line Count:', count)
    • Output:
      Line Count: 132045

Reading Entire Files

  • Reading All Content: You can read an entire file into a single string including newline characters:
  fhand = open('mbox-short.txt')
  inp = fhand.read()
  print(len(inp))  # 94626
  print(inp[:20])

Filtering Lines with Conditions

  • Searching Lines: Use an if statement to only print lines meeting specific criteria:
    python fhand = open('mbox-short.txt') for line in fhand: if line.startswith('From:'): print(line)
  • Stripping Newline Characters: You can use rstrip() to remove unnecessary newline characters before printing:
    python for line in fhand: line = line.rstrip() # Removes trailing whitespace if line.startswith('From:'): print(line)

Skipping Lines and Finding Substrings

  • Skipping with continue:

    for line in fhand:
        line = line.rstrip()
        if not line.startswith('From:'):
            continue
        print(line)
    
    • This method allows processing only desired lines based on specific conditions.
  • Using in for String Search: Check for the presence of a substring anywhere in a line:
    python for line in fhand: line = line.rstrip() if not '@uct.ac.za' in line: continue print(line)

Robustness in File Handling

  • Using Try/Except for error management while opening files:
    python fname = input('Enter the file name: ') try: fhand = open(fname) except: print('File cannot be opened:', fname) quit()
  • Count specific lines after error handling:
    python count = 0 for line in fhand: if line.startswith('Subject:'): count += 1 print('There were', count, 'subject lines in', fname)

Writing to Files

  • write() Method: To write to a writable file:
    python filehandle = open('example.txt', 'w') line1 = 'INSY5336 Intro to Python' filehandle.write(line1) filehandle.close()
  • Inserting Lines in a File:
    1. Read the content into a list.
    2. Modify the list as needed.
    3. Write the updated list back to the file.
      python filehandle = open('marytext.txt', 'r') lines = filehandle.readlines() filehandle.close() lines[5] = 'INSY5336\n' filehandle = open('marytext.txt', 'w') filehandle.writelines(lines) filehandle.close()

Summary

  • Key concepts include:
    • File Processing
    • Newline Character
    • File Handle
    • File Opening Techniques
    • Counting Lines
    • Reading Full Files
    • Searching/Selecting Lines
    • Robustness in Processing
    • Writing/Inserting Data into Files

Future Tasks

  • Review Chapters 7 & 8.
  • Execute relevant code snippets and exercises mentioned in the book.
  • Begin working on Homework 2.