import argparse
import os
import subprocess
import random

class VideoPickerException(Exception):
    '''Root Exception for the video_picker module'''
    def __init__(self, msg):
        self.msg = msg
    def __str__(self):
        return "{}: {}".format(self.__class__.__name__, self.msg)

class NoVideosException(VideoPickerException):
    '''Exception for video_picker module. Raised when there are no
    compatible video files found in the chosen directory.
    '''
    def __init__(self):
        VideoPickerException.__init__(self, 
                                      "No compatible video files found in given directory.")

class UnsupportedOSException(VideoPickerException):
    '''Exception for video_picker module. Raised when attempting 'pick' on an
    unsupported OS.
    '''
    def __init__(self):
        VideoPickerException.__init__(self, 
                                      "Unsupported operating system, unsure how to launch video.")

def pick(directory, recursive=False, video_types=['.avi', '.mp4', '.wmv', '.mkv']):
    '''Randomly chooses and plays a video file in the given directory.
       With 'recursive', starts at specified directory and includes all
       files underneath it. Modify video_types in this script to include other file types.
    '''     
    videos = []
    
    #gather files recursively or just in the given directory
    if recursive:
        for root, dirs, files in os.walk(args.directory):
            videos+=[os.path.join(root, f) for f in files]
    else:
        videos = os.listdir(directory)
    
    #take only those whose extensions are in video_types        
    videos = [v for v in videos if v[-4:].lower() in video_types]
    if len(videos) == 0:
        raise NoVideosException()
    
    #choose a random video        
    v = videos[random.randint(0, len(videos)-1)]
    filepath = os.join(directory, v)
    
    #figure out how to play it
    if os.name == 'nt':    
        os.startfile(filepath)
    elif os.name == 'posix':
        subprocess.call(('xdg-open', filepath))
    else:
        raise UnsupportedOSException()
        
            
if __name__ == '__main__':
    desc = """Randomly chooses and plays a video file in the given directory.
           With --recursive, starts at specified directory and includes all
           files underneath it. Modify VIDEO_TYPES in this script to include other file types."""
    
    parser = argparse.ArgumentParser(description=desc)
    parser.add_argument("directory", help="""The random choice will be made among videos in this directory. 
                                          If --recursive is set, will also include all subdirectories.""")
    parser.add_argument("-r", "-R", "--recursive", help="Include all videos in subdirectories of 'directory'", action="store_true")
    args = parser.parse_args()
    
    try:
        pick(args.directory, args.recursive)
    except NoVideosException as e:
        print("Error: {}".format(e.msg))
    except UnsupportedOSException as e:
        print("Error: {}".format(e.msg))

@@ -1005,17 +1005,17 @@
 o_types=
-%5B
+(
 '.avi', 
@@ -1036,17 +1036,17 @@
 , '.mkv'
-%5D
+)
 ):%0A    '
@@ -1554,24 +1554,113 @@
 ctory)%0A    %0A
+    #ensure extensions are lower case%0A    video_types = %5Bt.lower() for t in video_types%5D%0A
     #take on
@@ -1666,13 +1666,14 @@
 nly 
-th
+vide
 os
-e
  who
@@ -1753,21 +1753,16 @@
 if v
-%5B-4:%5D
 .lower()
  in 
@@ -1757,20 +1757,32 @@
 .lower()
- in 
+.endswith(tuple(
 video_ty
@@ -1780,24 +1780,26 @@
 (video_types
+))
 %5D%0A    if len
@@ -1936,16 +1936,16 @@
 os)-1)%5D%0A
-
     file
@@ -1954,16 +1954,21 @@
 th = os.
+path.
 join(dir
@@ -3013,97 +3013,19 @@
 ept 
-No
 Video
-sException as e:%0A        print(%22Error: %7B%7D%22.format(e.msg))%0A    except UnsupportedOS
+Picker
 Exce

Use string.endswith() for readability Handle parent exception instead of each child, accept a tuple instead of a list for video_types since it will not be changed