Opening Files

Open files with the open() function. Pass it a string path for file you want to open. Returns a File object.

i>>> from pathlib import Path
i>>> helloFile = open(Path.home() / 'hello.txt')
i>>> helloFile
<_io.TextIOWrapper name='/home/akraker/hello.txt' mode='r' encoding='UTF-8'>
i>>> helloFile = open('/home/akraker/hello.txt')
i>>> helloFile
<_io.TextIOWrapper name='/home/akraker/hello.txt' mode='r' encoding='UTF-8'>

Note, that you can only pass a Path object to the open() function as of Python 3.6+.

The open() function opens files in read-only mode by default.