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

(singke) #1
name of the removable drive’s device file. The first clue is to look for the name of directory
you recorded in step 3. In this example, the directory name is /media/28B3-27DC. Do you
see it on line 6 of Listing 23.1? The next clue is to look for the word /dev on the same line as
the directory name. In this example, the device file name is /dev/sda1, also listed on line 6.


  1. Record the device file name for your removable hard drive that you found in step 6. You will
    need this information in your Python presentation script.


Watch Out!: Changing Device File Names
Device file names can change! This is especially true if you have other removable hard
drives or devices attached to the USB ports on a Raspberry Pi. They can also change if
you plug your removable hard drive into a different USB port.


  1. Type umount device file name, where device file name is the device file name
    you recorded in step 6. (Yes, that command is umount and not unmount.) This command
    unmounts your removable hard drive from the Raspberry Pi. You can then safely remove it from
    the USB port.

  2. Type mkdir /home/pi/pictures to create a directory for your removable hard drive’s
    files.


Now that you have determined the device file name Raspbian will use to refer to your removable
hard drive, you need to make some modifications to your presentation script. First, create a variable
in your Python script to represent the device file name you determined in the steps above. Here’s an
example:


PictureDisk = '/dev/sda1'

The os module has a nice little function called .system. This function allows you to pass dash
shell commands (which you learned about in Hour 2) from your Python script to the operating system.


In your script, you should make sure that the removable hard drive has not been automatically
mounted. You can do this by issuing an umount command, using os.system, as shown here:


Click here to view code image


Command = "sudo umount " + PictureDisk
os.system(Command)

However, if the removable hard drive has not been mounted, this command generates an error
message and then continues on with the presentation script. Getting this error message is not a
problem. However, error messages certainly are not nice looking in a presentation! Not to worry:
You can hide the potential error message by using a little dash shell command trick, as shown here:


Click here to view code image


Command = "sudo umount " + PictureDisk + " 2>/dev/null"
os.system(Command)

Now to mount your removable hard drive using the Python script, you use the os.system function
again to pass a mount command to the operating system, like this:


Click here to view code image


Command = "sudo mount -t vfat " + PictureDisk + " " + PictureDirectory
Free download pdf