#!/usr/bin/env python3 import argparse import random import os import subprocess def main(): parser = argparse.ArgumentParser( prog='shuffle', description='play audio files in a directory in random order') parser.add_argument( 'path', default='.', nargs='?', help= 'play files in , or the current directory if is not passed') parser.add_argument('-m', dest='mpv_opts', default=['--no-video'], action='append', help='pass to mpv') args = parser.parse_args() # Keep track of the last few files we played and avoid playing them again prev = [] while True: files = [ os.path.join(root, file) for root, _, files in os.walk(args.path) for file in files ] for f in prev: files.remove(f) file = random.choice(files) prev.append(file) del prev[:len(prev) - 2] print('Playing', file) subprocess.run(['mpv', *args.mpv_opts, file]) if __name__ == "__main__": try: main() except KeyboardInterrupt: pass