Python - File File Opening


To open a file, use the built-in open function, with the external filename and a processing mode.

The call returns a file object, which has methods for data transfer:

afile = open(filename , mode) 

filename may include a platform-specific and absolute or relative directory path prefix.

Without a directory path, the file is assumed to exist in the current working directory, i.e., where the script runs.

The filename may contain non-ASCII Unicode characters.

The processing mode is typically the string:

Mode Description
'r' open for text input (default)
'w' create and open for text output
'a' open for appending text to the end.

Adding 'b' to the mode string for binary data.

Adding '+' opens the file for both input and output.