7.2. File Access Modes

7.2.1. Rationale

  • Text - easy to read and write

  • Binary - Fast and efficient

  • Read - Get data from file

  • Write - Save data to file (overwrite existing data)

  • Append - Add line to file

  • Update - Read and Write

  • If mode is not specified it will read in text mode (mode='rt')

FILE = r'/tmp/myfile.txt'

with open(FILE) as file:
    ...

7.2.2. Text Mode

  • Text - easy to read and write

  • mode='rt' - read in text mode (default)

  • mode='wt' - write in text mode

  • mode='at' - append in text mode

FILE = r'/tmp/myfile.txt'

with open(FILE, mode='rt') as file:
    ...

with open(FILE, mode='wt') as file:
    ...

with open(FILE, mode='at') as file:
    ...

7.2.3. Binary Mode

  • Binary - Fast and efficient

  • mode='rb' - read in binary mode

  • mode='wb' - write in binary mode

  • mode='ab' - append in binary mode

FILE = r'/tmp/myfile.txt'

with open(FILE, mode='rb') as file:
    ...

with open(FILE, mode='wb') as file:
    ...

with open(FILE, mode='ab') as file:
    ...

7.2.4. Update in Text Mode

  • Reading and Writing

  • mode='rt+' - update in binary mode

  • mode='wt+' - update in binary mode

  • mode='at+' - update in binary mode

FILE = r'/tmp/myfile.txt'

with open(FILE, mode='rt+') as file:
    ...

with open(FILE, mode='wt+') as file:
    ...

with open(FILE, mode='at+') as file:
    ...

7.2.5. Update in Binary Mode

  • Reading and Writing

  • mode='rb+' - update in binary mode

  • mode='wb+' - update in binary mode

  • mode='ab+' - update in binary mode

FILE = r'/tmp/myfile.txt'

with open(FILE, mode='rb+') as file:
    ...

with open(FILE, mode='wb+') as file:
    ...

with open(FILE, mode='ab+') as file:
    ...

7.2.6. Short Notation

  • By default text mode is used

  • Most commonly used

  • mode='r' - read in text mode

  • mode='w' - write in text mode

  • mode='a' - append in text mode

FILE = r'/tmp/myfile.txt'

with open(FILE, mode='r') as file:
    ...

with open(FILE, mode='w') as file:
    ...

with open(FILE, mode='a') as file:
    ...

7.2.7. Short Notation Update Mode

  • By default text mode is used

  • mode='r+' - read in text mode

  • mode='w+' - write in text mode

  • mode='a+' - append in text mode

FILE = r'/tmp/myfile.txt'

with open(FILE, mode='r+') as file:
    ...

with open(FILE, mode='w+') as file:
    ...

with open(FILE, mode='a+') as file:
    ...

7.2.8. Recap

  • mode='r' - read in text mode

  • mode='w' - write in text mode

  • mode='a' - append in text mode

  • mode='rt' - read in text mode (default)

  • mode='wt' - write in text mode

  • mode='at' - append in text mode

  • mode='rb' - read in binary mode

  • mode='wb' - write in binary mode

  • mode='ab' - append in binary mode

  • mode='rb+' - update in binary mode

  • mode='wb+' - update in binary mode

  • mode='ab+' - update in binary mode

  • mode='r+' - read in text mode

  • mode='w+' - write in text mode

  • mode='a+' - append in text mode

  • If mode is not specified it will read in text mode (mode='rt')

7.2.9. Exception Handling

FILE = r'/tmp/myfile.txt'

try:
    with open(FILE) as file:
        print(file.read())

except FileNotFoundError:
    print('File does not exist')

except PermissionError:
    print('Permission denied')

7.2.10. Assignments

7.2.10.1. File Access Error

English
  1. Using input() ask user for a file path

  2. Print file content

  3. Handle exception for not existing file

  4. Handle exception for not having sufficient permissions

Polish
  1. Używając input() zapytaj użytkownika o ścieżkę do pliku

  2. Wypisz zawartość pliku

  3. Obsłuż wyjątek dla nieistniejącego pliku

  4. Obsłuż wyjątek dla braku wystarczających uprawnień