The open() function is used to open a file for reading or writing or both.

Syntax:

open(file, mode='r', buffering = -1, encoding = None, error = None, newline=None, closefd=True, opener=None )

The most essential of this arguments is the the file and the mode. The required file argument is typically a string representing the path(relative or absolute) to the file to be opened. The argument can also be an integer which represents a file descriptor such as 0 for standard input, however, this is less common in everyday programming and is only used in low-level operations.

The optional mode argument tells the interpreter how to open the file. This argument determines whether the file is opened for reading or writing or both. The mode also determines whether the file is opened as a plain text file or a binary file. It defaults to 'r'​​​​​​ meaning that the file is opened as a text file for reading . The following table summarizes some of the the modes and their usage.

mode name usage
'r'/'rt' Read text Used to only read data from the file.
'w'/'wt' Write text Used to write data into the file or modify it. Remember write mode overwrites the data present in the file.
'a'/'at' Append  text Used to append data to the file. Remember data will be appended at the end of the file pointer.
'x'/'xt' exclusive creation-text Similar to opening the file in 'w' mode, except that an error is raised if a file with the specified name already exists
'r+'/'rt+' Read or Write text Used to write or read the data from the same file
'a+'/'at+' Append or Read text Used to read data from the file or append the data into the same file.
'rb' read binary Used to open a binary file for reading. It indicates that the file will be read in binary format, allowing you to retrieve the raw data from the file. 
'wb' write binary Used to open a binary file for writing. It indicates that you will be writing binary data to the file, allowing you to create or overwrite the file with new binary data.
'ab' append binary Similar to "wb", but it appends binary data to an existing file instead of overwriting it. It allows you to add new data at the end of the file without modifying the existing contents.
'rb+' read and write binary This mode allows you to open a binary file for both reading and writing. It enables you to read and modify the existing binary data within the file.

The file and the mode arguments are typically sufficient for most file operations. The minified syntax without including other arguments is as follows:

open(file, mode = "r")

The additional arguments can be useful in some rare scenarios.

  • The optional buffering integer argument specifies the buffering policy to be applied to the opened file. It can be a 0 to disable buffering, 1 to line buffering, or a value greater than 1 for a specific buffer size. By default, buffering is determined by the underlying file system.

  • The encoding argument specifies the encoding to use when reading or writing to the file. This argument is only relevant when working with text mode and helps handle different character encodings. The default encoding is platform-dependent.

  • The error argument specifies how encoding and decoding errors should be handled. Common values include 'strict' (raise an error), 'ignore' (ignore errors), or 'replace' (replace errors with a placeholder character).

  • The newline argument controls how line endings are handled when working with text files. It can be None (use the system default), '' (do not translate line endings), '\n' (translate to '\n'), '\r' (translate to '\r'), or '\r\n' (translate to '\r\n').

  • closefd: When set to False, it specifies that the underlying file descriptor should not be closed when the file is closed. By default, closefd is True, and the file descriptor is closed. This argument is only applicable in binary mode

The open function returns a file object which acts as a wrapper around the file; it contains methods and attributes for interacting and manipulating the file. It stores information such as its name, cursor's position in the file, current mode (read, write, append, etc.).

Example:

I have a file in my working directory named 'demo.txt' with the following data:

Up and down the City Road,
In and out the Eagle,
That's the way the money goes,
Pop! Goes the weasel.

Every night when I go out,
The monkey's on the table,
Take a stick and knock it off,
Pop! Goes the weasel.

Opening the file

file_object = open('demo.txt')
print(file_object)
//<_io.TextIOWrapper name='demo.txt' mode='r' encoding='cp1252'>
file.close()

Closing the file by calling the file_object.close() method is an essential part after you are done working with the file.  When you close the file, it releases the file handle so that the operating system can reclaim the resources and other programs can have access to the file.

We have already seen and used one method of the file object i.e close(). The dir() function returns all the names associated with an object including methods and attributes. We can use it to view all the methods and attributes of the file object.

​file_object = open('demo.txt')
list(filter(lambda x: not x.startswith('__'), dir(file_object)))
//['_CHUNK_SIZE', '_checkClosed', '_checkReadable', '_checkSeekable', '_checkWritable',
//'_finalizing', 'buffer', 'close', 'closed', 'detach', 'encoding', 'errors', 'fileno', 'flush', 'isatty',
// 'line_buffering', 'mode', 'name', 'newlines', 'read', 'readable', 'readline', 'readlines', 
//'reconfigure', 'seek', 'seekable', 'tell', 'truncate', 'writable', 'write', 'write_through', 
//'writelines'
file.close()

We can use extract information on the opened file using some attributes and methods. For example:

file = open('demo.txt')
file.name
//'demo.txt'
file.mode
//'r'
file.closed #is the file closed?
//False
file.encoding
//'cp1252'
file.writable()  #Is the file writable? 
//False
file.seekable()
//True
file.errors
//'strict'
file.close()
file.closed()
//True

The with statement simplifies exception handling by encapsulating common try/except statements in a single structure. It allows programmers to declare the resources that need to be acquired and released, and automatically releases those resources when the statement is finished.  By using this statement with the open() function, we can eliminate the need to explicitly call the close() function because it will be done for us by the interpreter in the background. Example:

with open('demo.txt') as file:
    print(file)
    print(file.name)
    print(file.writable())

//<_io.TextIOWrapper name='demo.txt' mode='r' encoding='cp1252'>
//demo.txt
//False

Using the with statement with the open function and generally where possible is considered a good practice in Python. This is because sometimes we might forget to call the close() method when we are done using the file. This can lead to data loss or corruption  if the program crashes before the file is closed.

Reading from a file

The read() method of the file object returns a string containing all of the contents of the opened file.  

Example:

with open('demo.txt', 'r') as file:
    file.read()

//"Up and down the City Road,\nIn and out the Eagle,\nThat's the way the money goes,\nPop! Goes the weasel.\n\nEvery night when I go out,\nThe monkey's on the table,\nTake a stick and knock it off,\nPop! Goes the weasel.\n"
with open('demo.txt', 'r') as file:
    print(file.read())

//Up and down the City Road,
//In and out the Eagle,
//That's the way the money goes,
//Pop! Goes the weasel.
//
//Every night when I go out,
//The monkey's on the table,
//Take a stick and knock it off,
//Pop! Goes the weasel.

The read() method also takes an optional argument which specifies how many characters it should read including the new line characters. It moves the file cursor at the position just before the last character to be read. If this  argument is not given like in above, then it will read the entire file meaning that the cursor will be moved t the end of the file.  If the method is called when the cursor is already at the end of the file, an empty string will be returned.

Example:

with open('demo.txt', 'r') as file:
    file.read(50)
    file.read(50)

//'Up and down the City Road,\nIn and out the Eagle,\nT'
//"hat's the way the money goes,\nPop! Goes the weasel"

The other reading methods include file.readline(), file.readlines(). Check on this article to see their usage.

The write method 

The write method is the basic tool for writing data to a file. It takes one required argument, which is the string to be written.

fileObject.write(string)

In order to write to a file, the file must be writable i.e opened in one of the modes which supports writing to the file such as 'w', 'a', 'wb' ,etc., otherwise a io.UnsupportedOperation exception will be raised. 

Example:

data = """
 A penny for a ball of thread
 Another for a needle,
 That's the way the money goes,
 Pop! Goes the weasel.

 All around the cobbler's bench
 the monkey chased the people;
 The donkey thought 'twas all in fun,
 Pop! Goes the weasel."""
with open('demo.txt', 'a') as file:
    file.write(data)

//233

The write function returns the number of characters written. 

You can check on working with text files  to see more on how the open() function is used with text files.

As mentioned earlier the open function can also be used with binary files. You can visit  read and write binary  for more information on binary operations with the function.