Code-Memo

File Modes

Basic File Modes

  1. 'r' - Read Mode:

    • Opens the file for reading.
    • The file pointer is placed at the beginning of the file.
    • This is the default mode.
    • If the file does not exist, an IOError is raised.
  2. 'w' - Write Mode:

    • Opens the file for writing.
    • Overwrites the file if it exists.
    • Creates a new file if it does not exist.
  3. 'a' - Append Mode:

    • Opens the file for writing.
    • The file pointer is at the end of the file if it exists.
    • Creates a new file if it does not exist.
    • New data is written at the end of the file.
  4. 'x' - Exclusive Creation Mode:

    • Creates a new file.
    • If the file already exists, an IOError is raised.

Text and Binary Modes

  1. Text Mode:

    • 't': This is the default mode. It handles the file as text.
    • Text mode is used for reading and writing strings.
  2. Binary Mode:

    • 'b': Handles the file as binary.
    • Binary mode is used for reading and writing bytes objects.

Combining Modes

  1. Read and Write Mode:

    • 'r+': Opens the file for both reading and writing.
    • The file pointer is placed at the beginning of the file.
    • Raises an IOError if the file does not exist.
  2. Write and Read Mode:

    • 'w+': Opens the file for both writing and reading.
    • Overwrites the existing file if it exists.
    • Creates a new file if it does not exist.
  3. Append and Read Mode:

    • 'a+': Opens the file for both appending and reading.
    • The file pointer is at the end of the file if it exists.
    • Creates a new file if it does not exist.

Advanced Details

  1. Binary Read and Write:

    • 'rb': Opens the file as binary for reading.
    • 'wb': Opens the file as binary for writing.
    • 'ab': Opens the file as binary for appending.
    • 'r+b' or 'rb+': Opens the file as binary for reading and writing.
    • 'w+b' or 'wb+': Opens the file as binary for writing and reading.
    • 'a+b' or 'ab+': Opens the file as binary for appending and reading.
  2. Newline Characters:

    • In text mode, newline characters (\n) are automatically converted to the system default.
    • In binary mode, newline characters are not converted.
  3. File Pointer:

    • The file pointer is an internal marker that tracks where the next read or write will occur.
    • Modes like 'a' and 'a+' place the file pointer at the end, so writes append data.
  4. Error Handling:

    • Trying to read a non-existent file in 'r' or 'r+' mode raises an IOError.
    • Trying to create an existing file in 'x' mode raises an IOError.

Practical Examples

  1. Reading a File:

    with open('example.txt', 'r') as file:
        content = file.read()
        print(content)
    
  2. Writing to a File:

    with open('example.txt', 'w') as file:
        file.write('Hello, World!')
    
  3. Appending to a File:

    with open('example.txt', 'a') as file:
        file.write('\nAppending this line.')
    
  4. Binary File Operations:

    with open('example.bin', 'wb') as file:
        file.write(b'\x00\x01\x02')