Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # make-car-usb.py
- # Quick little utility to copy files to a USB stick in alphabetical
- # order. Necessary because the genius programmers at Honda decided
- # the stereo would read the folders and files not in alphabetical
- # order, but in the order they were *written* to the stick. So this
- # just works around that by putting things on there one after another
- # in correct alphabetical order.
- # By Powered Descent of the Something Awful forums, 2018-11-23
- # Written on a Linux box but pretty much all you should need to do to
- # adapt to another platform is change the source and destination
- # locations, and if necessary changing the slashes to backslashes
- # in the very last line.
- # To use, arrange the folders and files you want to end up on the USB
- # in a folder somewhere and set the "source" variable below to that
- # folder, then set the "destination" to wherever the USB drive is.
- # IMPORTANT: This script will only go one folder deep below the "source",
- # since my car only understands one layer of folders! Any folders deeper
- # than that will be skipped.
- import os
- from shutil import copyfile, rmtree
- # Note the necessary trailing slashes!
- source="/home/[username]/Desktop/MusicForCar/"
- destination="/media/[username]/CARMUSIC/"
- # Get the okay from the user to blow stuff away.
- goforlaunch = raw_input("WARNING! This operation will erase everything in " + destination + "! Proceed (y/N): ")
- if (len(goforlaunch) == 0 or goforlaunch.lower()[0] != "y"):
- print("Aborting.")
- exit(1)
- # Clear out the destination. (Python doesn't seem to have a built-in
- # easy way to recursively delete the contents of a folder but not the
- # folder itself...)
- stufftodelete = os.listdir(destination)
- for doomedthing in stufftodelete:
- if (os.path.isdir(destination + doomedthing)):
- rmtree (destination + doomedthing)
- elif (os.path.isfile(destination + doomedthing)):
- os.remove(destination + doomedthing)
- else:
- # A link or a mount or something weird. Whatever, leave it alone.
- pass
- # On to the actual point. Make directories and copy files one-by-one
- # in the correct order.
- folderlist = os.listdir(source)
- folderlist = sorted(folderlist, key=lambda s: s.lower()) # Case-insensitive sort
- for folder in folderlist:
- print folder
- os.mkdir(destination + folder)
- filelist = os.listdir(source + folder)
- filelist = sorted(filelist, key=lambda s: s.lower()) # Case-insensitive sort
- for filename in filelist:
- print folder + '/' + filename
- copyfile(source + folder + '/' + filename, destination + folder + '/' + filename)
- # That's all, folks!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement