General information
Files are a basic data structure used in every programming language. In Python handling Files is especially easy.
Basic Syntax
Open Files
To open files we use the open-command:
open(filename, access-mode)
Closing files
To close files, we use (well, what else) the close-command:
close()
Writing information
To write information to a file, we use the write-command:
write('String_to_write_to_file')
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
Like the write-command, there exists the read-command:
read()
Access-mode
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. |