Skip to content

Instantly share code, notes, and snippets.

@teward
Last active March 3, 2017 02:36
Show Gist options
  • Select an option

  • Save teward/440ef1dc7c91b8d593f3cbed3eef991a to your computer and use it in GitHub Desktop.

Select an option

Save teward/440ef1dc7c91b8d593f3cbed3eef991a to your computer and use it in GitHub Desktop.

Revisions

  1. teward revised this gist Mar 3, 2017. 1 changed file with 25 additions and 25 deletions.
    50 changes: 25 additions & 25 deletions randommp3
    Original file line number Diff line number Diff line change
    @@ -7,32 +7,32 @@ import glob
    import random
    import argparse

    # Parse arguments to the script
    argparser = argparse.ArgumentParser(description="Open a random MP3 in the player of choice, or the default player",
    add_help=True)
    argparser.add_argument('--dir', '--directory', '--music-dir', dest='dirpath', type=str,
    default=str('/home/' + getpass.getuser() + '/Music'), required=False,
    help="The path to the directory where your music is stored. If the path has spaces, wrap the "
    "entire path in single-quotes ('/home/ubuntu/My Music/' for example). If not specified, "
    "the current user's Music directory in their /home/ folder is used.")
    argparser.add_argument('player', type=str, help="The executable name of the media player "
    "to open the MP3 with. If none specified, "
    "uses the system default player.")
    if __name__ == "__main__":
    # Parse arguments to the script
    argparser = argparse.ArgumentParser(description="Open a random MP3 in the player of choice, or the default player",
    add_help=True)
    argparser.add_argument('--dir', '--directory', '--music-dir', dest='dirpath', type=str,
    default=str('/home/' + getpass.getuser() + '/Music'), required=False,
    help="The path to the directory where your music is stored. If the path has spaces, wrap the "
    "entire path in single-quotes ('/home/ubuntu/My Music/' for example). If not specified, "
    "the current user's Music directory in their /home/ folder is used.")
    argparser.add_argument('player', type=str, help="The executable name of the media player "
    "to open the MP3 with. If none specified, "
    "uses the system default player.")

    # Using the above 'argparser' items, get the arguments for what we're going to be using.
    args = argparser.parse_args()
    # Using the above 'argparser' items, get the arguments for what we're going to be using.
    args = argparser.parse_args()

    # Gp to the directory your music is in.
    os.chdir(args.dirpath)
    mp3s = glob.glob('*.mp3')
    # Gp to the directory your music is in.
    os.chdir(args.dirpath)
    mp3s = glob.glob('*.mp3')

    # Modify the directory path to make sure we have the trailing slash
    dirpath = args.dirpath
    if dirpath[-1] not in '/\\':
    dirpath += '/'
    # Modify the directory path to make sure we have the trailing slash
    dirpath = args.dirpath
    if dirpath[-1] not in '/\\':
    dirpath += '/'

    # Actually open the MP3 file, and /dev/null to suppress output messages from the process.
    DEV_NULL = open(os.devnull, 'w')
    execpath = [args.player, '%s%s' % (dirpath, str(random.choice(mp3s)))]
    print execpath
    sp.Popen(execpath, stdout=DEV_NULL, stderr=DEV_NULL)
    # Actually open the MP3 file, and /dev/null to suppress output messages from the process.
    DEV_NULL = open(os.devnull, 'w')
    execpath = [args.player, '%s%s' % (dirpath, str(random.choice(mp3s)))]
    sp.Popen(execpath, stdout=DEV_NULL, stderr=DEV_NULL)
  2. teward revised this gist Mar 3, 2017. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions randommp3
    Original file line number Diff line number Diff line change
    @@ -7,8 +7,7 @@ import glob
    import random
    import argparse

    DEV_NULL = open(os.devnull, 'w')

    # Parse arguments to the script
    argparser = argparse.ArgumentParser(description="Open a random MP3 in the player of choice, or the default player",
    add_help=True)
    argparser.add_argument('--dir', '--directory', '--music-dir', dest='dirpath', type=str,
    @@ -20,6 +19,7 @@ argparser.add_argument('player', type=str, help="The executable name of the medi
    "to open the MP3 with. If none specified, "
    "uses the system default player.")

    # Using the above 'argparser' items, get the arguments for what we're going to be using.
    args = argparser.parse_args()

    # Gp to the directory your music is in.
    @@ -31,6 +31,8 @@ dirpath = args.dirpath
    if dirpath[-1] not in '/\\':
    dirpath += '/'

    # Actually open the MP3 file, and /dev/null to suppress output messages from the process.
    DEV_NULL = open(os.devnull, 'w')
    execpath = [args.player, '%s%s' % (dirpath, str(random.choice(mp3s)))]
    print execpath
    sp.Popen(execpath, stdout=DEV_NULL, stderr=DEV_NULL)
  3. teward renamed this gist Mar 3, 2017. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. teward created this gist Mar 3, 2017.
    36 changes: 36 additions & 0 deletions randommp3.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    #!/usr/bin/python

    import getpass
    import subprocess as sp
    import os
    import glob
    import random
    import argparse

    DEV_NULL = open(os.devnull, 'w')

    argparser = argparse.ArgumentParser(description="Open a random MP3 in the player of choice, or the default player",
    add_help=True)
    argparser.add_argument('--dir', '--directory', '--music-dir', dest='dirpath', type=str,
    default=str('/home/' + getpass.getuser() + '/Music'), required=False,
    help="The path to the directory where your music is stored. If the path has spaces, wrap the "
    "entire path in single-quotes ('/home/ubuntu/My Music/' for example). If not specified, "
    "the current user's Music directory in their /home/ folder is used.")
    argparser.add_argument('player', type=str, help="The executable name of the media player "
    "to open the MP3 with. If none specified, "
    "uses the system default player.")

    args = argparser.parse_args()

    # Gp to the directory your music is in.
    os.chdir(args.dirpath)
    mp3s = glob.glob('*.mp3')

    # Modify the directory path to make sure we have the trailing slash
    dirpath = args.dirpath
    if dirpath[-1] not in '/\\':
    dirpath += '/'

    execpath = [args.player, '%s%s' % (dirpath, str(random.choice(mp3s)))]
    print execpath
    sp.Popen(execpath, stdout=DEV_NULL, stderr=DEV_NULL)