Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23131

emulating a shell with commands

$
0
0

i am making a discord bot, and as part of the bot, i want to create the option to emulate a shell (allowing the user to run commands such as ls, cd etc on the host of the bot). i tried learning how to use subprocess from the python docs, and came up with this class:

class CommandLine:    def __init__(self, command: str):       self.process = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)    def run(self):       self.process.wait()       # some processes don't have an exit code, so i check if there's an error instead       if len(self.process.stderr) == 0:           return self.process.stdout       else:           return self.process.stderr    def close(self):       self.process.stdin.close()

this class works for running one command at a time, but i can't chain multiple commands that depend on the previous commands (for example, any command that depends on the current directory can't be used because cd doesn't do antyhing). how can i run multiple commands, one after the other, without creating a new subprocess for every one of them?

note: since my goal is simply to emulate a shell, i don't really care about the module being used. if there's a better module or way to do this thing, i would prefer to use that


Viewing all articles
Browse latest Browse all 23131

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>