Octa
Octa

Reputation: 119

Python Subprocess.run Seemingly does Nothing

I am writing a wrapper script to make generation of random maps for a game more convenient. The idea is to be able to let the game generate a bunch of maps while I go do something else (as it can take quite a while with large maps). Here is the script: https://pastebin.com/MSLc9C3J

The important part is:

command_str = ['Dominions5.exe', '--makemap', f'{args.mapname}']

    command_str.append('--mapsize')
    command_str.append(str(args.width))
    command_str.append(str(args.height))

    exclude_args = ['mapname', 'width', 'height', 'hwrap', 'vwrap', 'nummaps']

    for arg in vars(args):
        if arg not in exclude_args:
            command_str.append('--' + arg)
            command_str.append(str(getattr(args, arg)))

    if not args.hwrap:
        command_str.append('--nohwrap')

    if args.vwrap:
        command_str.append('--vwrap')

    nummaps = args.nummaps

    for x in range(nummaps):
        command_str[2] = args.mapname if x == 0 else args.mapname + str(x)
        cmd = ' '.join(command_str)
        subprocess.run(split(cmd), shell=True, capture_output=True)

Running this script on the command-line produces no output, and I can see no instances of Dominions5 running in taskmanager. I have tried passing the run arguments differently, (with the cmd as a string, without shell-True, etc.) but have not gotten a different result. I have also tried doing the same with Popen (which I would like to try to get working as well, just to see how well this works when the maps are generated in parallel).

How do I make subprocess.run work as I want?

Upvotes: 0

Views: 148

Answers (1)

Alberto Garcia
Alberto Garcia

Reputation: 339

2 things that may help:

  1. if you are going to split your command into a list of strings, then make shell = False, if you use the command as a whole (a single string), use shell = True. So...

    either: subprocess.run(split(cmd), shell = False... or subprocess.run(command_str, shell = True...

  2. if you want to see the standard output of your subprocess.run either

    • make capture_output = False or,

    • capture the output and print it after command execution:

c = subprocess.run(command_str, shell=True, capture_output=True)
print(c.stdout)

Upvotes: 1

Related Questions