This program was not specifically written for a class that I teach, but I may use it in a future class. This is a fairly simple program that demonstrates working with drives, directories and files. The usage of a number of Python modules is demonstrated. These include: string, random, sys, os, os.path, shutil and getopt. Additionally, getoutput is used in the Unix environment and pywin32 is used when used on a Windows based computer. This program does not use object oriented programming, so it is suitable for study by beginning students.
The motivation for writing this program was that periodically I want to transfer a random sampling of music to a basic mp3 player. I used to just transfer music files until it is full, but that is tedious and time consuming; I end up with some of the same songs; and the last copy fails when it is full.
First, generate a list of songs that might be transfered to the player. I then edited the list to remove some songs (file names) I don’t want to listen to. (My daughter added some songs that I don’t like – such is life.) Working with a list of files from a file also allows for some customization of which songs end up on the MP3 player (example of that below).
You need to want to pay attention to the absolute path names of the files. My music files all reside in a directory under my home directory named Music and in the root directory of the player is a directory named Music, which is the destination directory for the music. So, I generate my list so that each line begins with Music/...:
cd
find Music -type f > allmp3s
vi allmp3s
Next, plug in the MP3 player and note its mount point. For this discussion, I’ll use a mount point of /mnt/player.
To fill the player with a new random sampling of songs, we can use the –fill option:
$ randmusic.py allmp3s /mnt/player --fill
Note
The randmusic.py program does not assume that the device should be completely filled. With no optional arguments (other than source and destination), will replace the song on the player filling approximately the same amount of space as was initially on the player.
If we know that we need to reserve a certain amount of space on the player for something we want to add to it later, we can use the –add to fill the player to a specific level. The used space on the player after the –add option is used is the current space used plus the value supplied with the –add option.
$ randmusic.py allmp3s /mnt/player --fill
Sometimes, you man not wish to remove any files on the player, but just want to add additional files to it. This is when the –keep option is used. The –keep option comes in handy to load certain songs and then fill the remaining space on the device with a random sampling of songs.
$ randmusic.py favorites /mnt/player
$ randmusic.py allmp3s /mnt/player --keep --fill
File: randmusic.py
Usage:
$ randmusic.py src dest [(-a | –add) x] [-f | –fill] [-k | –keep]
or
$ randmusic.py –help
Copy a random set of files, which have a collective size just under the capacity of a device such as a mp3 player.
Author: Tim Bower
Kansas State University at Salina
Apache Open Source License, V2.0
| Parameters: |
|
|---|
The driving function for updating the mp3 player with new files.