Notes on File Handling in Python

1

Introduction to File Handling

  • File Handling: Allows reading from and writing to disk files in Python. It enables data storage beyond program execution.

Data Files

  • Text Files:
    • Store data as ASCII or Unicode characters.
    • Example: "computer" takes 8 bytes, 11237.9876 takes 10 bytes.
    • End-of-Line (EOL) characters: '\n' or '\r'.
  • Binary Files:
    • Store data in its raw memory format (e.g., integers, floats).
    • No EOL delimiters.
    • Faster and more efficient but requires specific reading through Python.

Steps in Data File Handling

  1. Opening Files:
    • Syntax: file_object = open(filename, mode)
    • Default mode: 'r' (read). Other modes include 'w' (write), 'a' (append).
  2. Performing Read/Write Operations:
    • Read:
      • file_object.read([n]): Reads n bytes or the entire file if n is omitted.
      • file_object.readline([n]): Reads a line or n bytes.
      • file_object.readlines(): Reads all lines into a list.
    • Write:
      • file_object.write(str): Writes string str to the file.
      • file_object.writelines(list): Writes a list of strings to the file.
  3. Closing Files:
    • Syntax: file_object.close()
    • Ensures data is saved and file resources are released.

File Modes and Paths

  • Opening File Modes:
    • 'r': Read (default).
    • 'w': Write (overwrites file).
    • 'a': Append (adds to the end).
  • File Path Syntax:
    • Absolute Path: Full path from the root, e.g., C:\SALES\2018\REVENUE.TXT.
    • Relative Path: Relative to the current directory, e.g., .\2019\SHEET.XLS.
    • Double Backslash (\): Used in paths to escape the backslash, e.g., d:\\mydata\\poem.txt.
    • Raw Strings: Use r before the string to treat backslashes literally, e.g., r"d:\mydata\poem.txt".

File Operations

  • File Handle: The reference to the file used for reading/writing operations.
  • Flush: file_object.flush() forces the buffer to write to the file.
  • Stripping Whitespace: Use .strip(), .lstrip(), or .rstrip() to remove whitespace from strings.

Standard Input, Output, and Error

  • Standard Input (stdin): Reads from the keyboard.
  • Standard Output (stdout): Writes to the monitor.
  • Standard Error (stderr): Writes errors to the monitor.
    • Using sys module:pythonCopy codeimport sys sys.stdin, sys.stdout, sys.stderr

Using with Statement

  • Context Manager:
    • Syntax: with open(filename, mode) as file_handle: # file operations
    • Automatically closes the file after the block of code.

Binary File Operations

  • Pickle Module:
    • Dump: pickle.dump(object, file_handle) writes an object to a binary file.
    • Load: pickle.load(file_handle) reads an object from a binary file.

Current Working Directory

  • Getting Current Directory:
    • import os pwd = os.getcwd() print("Current Directory:", pwd)

We will be happy to hear your thoughts

Leave a reply

Captcha

ScienceShala
Logo
Enable registration in settings - general