To open a file, you can use Python's built-in open() function:
file = open("<relative/path/to/file>", "<permission>")
The variable named file in the previous example is what the documentation calls a file object. A file object doesn't actually contain the contents of the file, rather it is used to access the file's location in memory and give the program permission to operate on the file. The file object is generally used as a parameter to other functions that read the data.
The first parameter of the open() function is a string containing the relative path from the directory in which the program is run to the file containing your data.
The second parameter is the permission, which is a string designating the level of permission that is granted to the program for working with a file. I will use two permissions in this book for file I/O:
- The read permission, designated by the r character, allows the program to read, but not to modify the contents of the file
- The write permission, designated by the w character, allows the program to write to the file and automatically erases the contents of any file that already exists with the specified file path