Opening File with the open()
Function
• The read mode is the default mode for files you open in Python.
• Without relying on Python’s default behaviour, you can explicitly
specify the mode by passing the string value ‘r’ as a second argument
to the function open().
• So open('/Users/chima/Documents/test/hello.txt’, ‘r’) and
open('/Users/chima/Documents/test/hello.txt’) do the same thing.
• The call to open() returns a File object. A File object represents a file
on your computer. It is another type of value in Python.
• In the previous example, we stored the File object in the variable
helloFile. Now, whenever you want to read from the file, you can do so
by calling methods on the File object stored in helloFile.
18