User Guide
Table Of Contents
150
Learning about Packages
Packages group modules together in namespaces – organized
and hierarchical trees of modules. Working with namespaces is a
little like working with the file system, except that you use dots
instead of slashes.
6.8 File I/O
Working with files and folders from Python is important in
Raspbian, because many of the system processes and devices
are mapped into the file system.
Opening a File
To read from or write to a file in Python, you need to open it
using the built-in function open(). In its basic form, open()
accepts two arguments:
open(filename, mode)
If the file is not in the same directory as your Python script, you
need to specify the full path to the file.
Mode is optional and if you do not specify it then Python opens
the file in read-only mode. This means that you cannot change
the file, only read data from it. The most-common values that are
used here are:
You use package names when importing modules and reading
their documentation. So you will become familiar with how this
works without having to make a special effort.
Mode Description
r Open the file for reading.
rb Open the file for reading in binary format.
w Open the file for writing only. This overwrites the
existing file or, if it does not exist, creates the file.
wb Open the file for writing only, in binary format.