How to do it...

  1. Import the subprocess module:
        import subprocess 
  1. Design the command line. Generally, this should be tested at the OS prompt to be sure that it does the right things. We've shown an example of the command.
  2. Define a generator for the various commands to be executed. Each command can be built as a sequence of individual words. The original shell command is split on spaces to create the sequence of words:
        def command_iter(files): 
            for n in range(files): 
                filename = 'game_{n}.yaml'.format_map(vars()) 
                command = ['python3', 'ch13_r05.py', 
                    '--samples', '10', '--output', filename] 
                yield command 

This generator will yield a sequence of command strings. A client can use a for statement to consume each of the generated commands.

Get Modern Python Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.