Advertisement
Guest User

make-car-usb.py

a guest
Dec 6th, 2019
325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.56 KB | None | 0 0
  1. # make-car-usb.py
  2.  
  3. # Quick little utility to copy files to a USB stick in alphabetical
  4. # order.  Necessary because the genius programmers at Honda decided
  5. # the stereo would read the folders and files not in alphabetical
  6. # order, but in the order they were *written* to the stick.  So this
  7. # just works around that by putting things on there one after another
  8. # in correct alphabetical order.
  9.  
  10. # By Powered Descent of the Something Awful forums, 2018-11-23
  11.  
  12. # Written on a Linux box but pretty much all you should need to do to
  13. # adapt to another platform is change the source and destination
  14. # locations, and if necessary changing the slashes to backslashes
  15. # in the very last line.
  16.  
  17. # To use, arrange the folders and files you want to end up on the USB
  18. # in a folder somewhere and set the "source" variable below to that
  19. # folder, then set the "destination" to wherever the USB drive is.
  20. # IMPORTANT: This script will only go one folder deep below the "source",
  21. # since my car only understands one layer of folders!  Any folders deeper
  22. # than that will be skipped.
  23.  
  24. import os
  25. from shutil import copyfile, rmtree
  26.  
  27. # Note the necessary trailing slashes!
  28. source="/home/[username]/Desktop/MusicForCar/"
  29. destination="/media/[username]/CARMUSIC/"
  30.  
  31. # Get the okay from the user to blow stuff away.
  32. goforlaunch = raw_input("WARNING!  This operation will erase everything in " + destination + "!  Proceed (y/N): ")
  33. if (len(goforlaunch) == 0 or goforlaunch.lower()[0] != "y"):
  34.     print("Aborting.")
  35.     exit(1)
  36.  
  37. # Clear out the destination.  (Python doesn't seem to have a built-in
  38. # easy way to recursively delete the contents of a folder but not the
  39. # folder itself...)
  40. stufftodelete = os.listdir(destination)
  41. for doomedthing in stufftodelete:
  42.     if (os.path.isdir(destination + doomedthing)):
  43.         rmtree (destination + doomedthing)
  44.     elif (os.path.isfile(destination + doomedthing)):
  45.         os.remove(destination + doomedthing)
  46.     else:
  47.         # A link or a mount or something weird.  Whatever, leave it alone.
  48.         pass
  49.  
  50. # On to the actual point.  Make directories and copy files one-by-one
  51. # in the correct order.
  52. folderlist = os.listdir(source)
  53. folderlist = sorted(folderlist, key=lambda s: s.lower()) # Case-insensitive sort
  54. for folder in folderlist:
  55.     print folder
  56.     os.mkdir(destination + folder)
  57.     filelist = os.listdir(source + folder)
  58.     filelist = sorted(filelist, key=lambda s: s.lower()) # Case-insensitive sort
  59.     for filename in filelist:
  60.         print folder + '/' + filename
  61.         copyfile(source + folder + '/' + filename, destination + folder + '/' + filename)
  62. # That's all, folks!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement