0

Well the first problem I run into is that I have no idea how to respond to a command prompt.

bat_location = "F:/SteamLibrary/steamapps/common/Terraria"
os.chdir(bat_location)
os.system("TerrariaServer.exe -steam -lobby friends -config serverconfig.txt")

all of this works, but then when I want to respond to the command prompt which asks me which world I want to run ( the worlds are indexed by numbers from 1 - n (amount of worlds)) I dont know how to respond to it.

I've looked all over google but the code doesnt seem to work.

So basically what I need is when a cmd asks me for example :

Choose World:

I want to automatically respond with the number 10.

os.system("10")

this doesnt seem to do anything, I've also tried a lot with subprocesses but im clearly lost.

Any help is apriciated!

EDIT NR.1 :

Welp, now I've tried this :

bat_location = r'F:\SteamLibrary\steamapps\common\Terraria'
with Popen('TerrariaServer.exe -steam -lobby friends -config serverconfig.txt',
    cwd=f'{bat_location}', stdin=PIPE, shell=True) as proc:
      proc.stdin.write(b'10\n')

and all it does, im guessing by the response, it just loops around and around.

EDIT NR.2 :

I WILL CLOSE THIS AND START A NEW THREAD, SINCE MY PROBLEM COMPLETELY DERIVED FROM THE ORIGINAL.

sun0nbeach
  • 25
  • 7
  • 1
    Take a look at the answers to ["Python subprocess and user interaction."](https://stackoverflow.com/questions/14457303/python-subprocess-and-user-interaction) You almost certainly want to be using [`subprocess`](https://docs.python.org/3.7/library/subprocess.html), not `os.system`. – jirassimok Oct 03 '19 at 01:47
  • ok so I tried using this : proc = subprocess.Popen(['cd F:/SteamLibrary/steamapps/common/Terraria', 'TerrariaServer.exe -steam -lobby friends -config serverconfig.txt'], stdout=PIPE, stdin=PIPE, stderr=STDOUT) but it just throws out an error that [WinError 2] The system cannot find the file specified Am i missing something?? – sun0nbeach Oct 03 '19 at 02:11
  • That's trying to run two different commands; the `subprocess` functions only run one command each. To set the working directory, use the `cwd` keyword argument. – jirassimok Oct 03 '19 at 02:23
  • When do i specify it before the "TerrariaServer.exe etc" or after? – sun0nbeach Oct 03 '19 at 02:34
  • `cwd` must be given after as a keyword argument, after any positional arguments. However, you might want to run the program by using the full path instead, which presumably is `F:/SteamLibrary/steamapps/common/Terraria/Terraria.exe`. – jirassimok Oct 03 '19 at 02:42
  • when I use Popen I dont seem to get resposes in the console, am I missing some stdout or something the code looks like this now and it runs but no output : proc = Popen('TerrariaServer.exe -steam -lobby friends -config serverconfig.txt', cwd='F:/SteamLibrary/steamapps/common/Terraria', stdout=PIPE, stdin=PIPE, stderr=STDOUT, shell=True) – sun0nbeach Oct 03 '19 at 02:47
  • If you are passing in `stdout=PIPE` and `stderr=PIPE`, the outputs of the process will be given as the `Popen` object's `stdout` and `stderr` properties, respectively, instead of being printed to the console. I'll try to write up a simple example as an answer. – jirassimok Oct 03 '19 at 02:50
  • ok this works now as I have just removed the stdin and stdout and it outputs the stuff, now back to the original problem how do I respond to a command prompt? – sun0nbeach Oct 03 '19 at 02:53
  • Im using pycharm and I cant even respond to it at the run command prompt. – sun0nbeach Oct 03 '19 at 02:57

1 Answers1

1

From your last few comments, I realized the problem you were having with Popen. When you pass stdout=PIPE and stderr=PIPE, the outputs of the process are captured by the pipes, so you won't see them unless you read from the pipes.

So here's a simple example that you should be able to work with:

import subprocess
from subprocess import PIPE
from textwrap import dedent

with open('tmp.py', 'w') as f:
    f.write(dedent("""
            print(input())
            print(input())
            """))

with subprocess.Popen(['python3', 'tmp.py'], stdin=PIPE) as proc:
    proc.stdin.write(b'Hello, world!\n') # write to the process' input
    proc.stdin.write(b'Good bye, world!\n') # write to the process' input

If you want to read the data from the function in Python, you can use stdout=PIPE, then use proc.stdout.read and the like, but you may have to be careful about how you get data from the blocking read functions.

jirassimok
  • 3,850
  • 2
  • 14
  • 23