Python Programming for Raspberry Pi, Sams Teach Yourself in 24 Hours

(singke) #1

The Linux directory structure is called an upside-down tree because the top of the directory structure
is called the root. Figure 11.1 shows the top root directory (/) with the subdirectories right beneath
it.


FIGURE 11.1 The subdirectories in the top root directory (/).

Each subdirectory stores particular files according to their purpose. Directory names are written in
two ways: as an absolute directory reference or as a relative directory reference.


An absolute directory reference always begins with the root directory. For example, when you log in
to your Raspberry Pi using the pi account, you are in the directory /home/pi. This is an absolute
directory reference because it starts with the root directory (/).


By the Way: Memory Trick
One way to remember that an absolute directory reference begins with the root
directory (/) is a simple memory sentence, like this: “Absolute directories absolutely
begin with the root directory.”

A relative directory reference does not begin with the root directory (/). Instead, it denotes a
directory relative to where your present working directory is now. Back in Hour 2, “Understanding
the Raspbian Linux Distribution,” you learned that a present working directory is where you are
currently located in the directory structure. You can see the present working directory by using the
pwd shell command. Listing 11.1 shows an example of using a relative directory reference.


LISTING 11.1 A Relative Directory Reference Example


Click here to view code image


pi@raspberrypi ~ $ pwd
/home/pi
pi@raspberrypi ~ $ ls py3prog
sample.py script0402.py script0702.py script0901.py script0903.py
script0401.py script0701.py script0703.py script0902.py script0904.py
pi@raspberrypi ~ $

In Listing 11.1, the pwd command is used to show the present working directory of the user pi. You
can see that the present working directory is /home/pi, which is an absolute directory reference.
Then the command ls py3prog is entered, in order to display the Python scripts currently located
within the py3prog subdirectory. The ls command uses a relative directory reference. To use an
absolute directory reference, the command would be ls /home/pi/py3prog.


To help you learn about using files in Python, the rest of this book uses the directories shown in Table

Free download pdf