General information Edit

Files are a basic data structure used in every programming language. In Python handling Files is especially easy.

Basic Syntax Edit

Open Files Edit

To open files we use the open-command:

open(filename, access-mode)

Closing files Edit

To close files, we use (well, what else) the close-command:


Writing information Edit

To write information to a file, we use the write-command:


Note that with the write-command, we can only write Strings to a file, for writing more complicated structures, please check out the Pickle module.

Reading informations Edit

Like the write-command, there exists the read-command:


Access-mode Edit

There are several different access-modes:

Access-mode Explanation
w Write-mode, overwrites data if file is non-empty
r Read-mode, read-only
a Append-mode, writes informations at the end of the file
For all of these modes there exists the same one with a '+', e.g. w+. This is read-and-write-mode.
For all of these also exists the same name with 'b', e.g. wb. This stands for binary-mode.
Community content is available under CC-BY-SA unless otherwise noted.