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

(singke) #1

11.2.


TABLE 11.2 Python Directories for This Book

Test yourself here. In Table 11.2, which type of directory reference is used: absolute or relative?


Managing Files and Directories via Python


You learned how to create directories in Hour 2. You were the one who created the
/home/pi/py3prog directory by using the mkdir shell command. Now you will learn how to
manage files and make directories by using a Python program!


Python comes with a multiplatform function called os. The os function allows you to conduct
various operating system functions, such as creating directories. Table 11.3 lists some of the os
methods you can use to manage files and directories in Python.


TABLE 11.3 A Few os Function Methods

The os function is not a built-in Python function. Therefore, you need to issue the Python statement
import os before you can use the methods listed in Table 11.3.


Did You Know: More os, Please
The os function has a great deal more methods than shown here. To learn about these
methods, go to docs.python.org/3/library/os.html.

Listing 11.2 shows a few of the os function’s methods being used. You can see on line 8 that the os
function is imported, and a new subdirectory is created on line 11. The present working directory is
then changed to the newly created subdirectory on line 12.

Free download pdf