Weird subprocess.run behaviour /u/Entraxipy Python Education
Weird subprocess.run behaviour /u/Entraxipy Python Education
So I’m making a small test project to learn python better and for some reason, when I run it, it prints the command out, and then it seems like subprocess.run
freezes as nothing else happens. When I copy the command and paste it in command prompt however, the command works perfectly fine. When an error is raised, the subprocess.run
returns a non-zero return code and my program continues as normal. What is happening?
command is typically structured as a multiline list of valid terminal commands.
import os import subprocess import gptinterface as ChatGPT import time initial_prompt = r""" ommited for clarity """ workfolder = r"C:UsersRoberPycharmProjectsAI_coderworkplace" command = ChatGPT.send_msg(initial_prompt) while True: command = command.strip("`").strip() print(">>> "+repr(command)+"") if command == "help": command = ChatGPT.send_msg(initial_prompt) continue start = time.time() result = subprocess.run(str(command), cwd=workfolder, shell=True, capture_output=True, env=os.environ, text=True, timeout=5) end = time.time() print("finished command") if result.returncode == 0: time_taken = (end-start)*1000 print(result.stdout) command = ChatGPT.send_msg(f"Success:n{result.stdout}nnTook {time_taken:.1f} msnSend 'help' if you want a reminder of your target.") else: print(result.stderr) command = ChatGPT.send_msg("Error:n"+result.stderr+"nSend 'help' if you want a reminder of your target.")
submitted by /u/Entraxipy
[link] [comments]
r/learnpython So I’m making a small test project to learn python better and for some reason, when I run it, it prints the command out, and then it seems like subprocess.run freezes as nothing else happens. When I copy the command and paste it in command prompt however, the command works perfectly fine. When an error is raised, the subprocess.run returns a non-zero return code and my program continues as normal. What is happening? command is typically structured as a multiline list of valid terminal commands. import os import subprocess import gptinterface as ChatGPT import time initial_prompt = r””” ommited for clarity “”” workfolder = r”C:UsersRoberPycharmProjectsAI_coderworkplace” command = ChatGPT.send_msg(initial_prompt) while True: command = command.strip(“`”).strip() print(“>>> “+repr(command)+””) if command == “help”: command = ChatGPT.send_msg(initial_prompt) continue start = time.time() result = subprocess.run(str(command), cwd=workfolder, shell=True, capture_output=True, env=os.environ, text=True, timeout=5) end = time.time() print(“finished command”) if result.returncode == 0: time_taken = (end-start)*1000 print(result.stdout) command = ChatGPT.send_msg(f”Success:n{result.stdout}nnTook {time_taken:.1f} msnSend ‘help’ if you want a reminder of your target.”) else: print(result.stderr) command = ChatGPT.send_msg(“Error:n”+result.stderr+”nSend ‘help’ if you want a reminder of your target.”) submitted by /u/Entraxipy [link] [comments]
So I’m making a small test project to learn python better and for some reason, when I run it, it prints the command out, and then it seems like subprocess.run
freezes as nothing else happens. When I copy the command and paste it in command prompt however, the command works perfectly fine. When an error is raised, the subprocess.run
returns a non-zero return code and my program continues as normal. What is happening?
command is typically structured as a multiline list of valid terminal commands.
import os import subprocess import gptinterface as ChatGPT import time initial_prompt = r""" ommited for clarity """ workfolder = r"C:UsersRoberPycharmProjectsAI_coderworkplace" command = ChatGPT.send_msg(initial_prompt) while True: command = command.strip("`").strip() print(">>> "+repr(command)+"") if command == "help": command = ChatGPT.send_msg(initial_prompt) continue start = time.time() result = subprocess.run(str(command), cwd=workfolder, shell=True, capture_output=True, env=os.environ, text=True, timeout=5) end = time.time() print("finished command") if result.returncode == 0: time_taken = (end-start)*1000 print(result.stdout) command = ChatGPT.send_msg(f"Success:n{result.stdout}nnTook {time_taken:.1f} msnSend 'help' if you want a reminder of your target.") else: print(result.stderr) command = ChatGPT.send_msg("Error:n"+result.stderr+"nSend 'help' if you want a reminder of your target.")
submitted by /u/Entraxipy
[link] [comments] So I’m making a small test project to learn python better and for some reason, when I run it, it prints the command out, and then it seems like subprocess.run freezes as nothing else happens. When I copy the command and paste it in command prompt however, the command works perfectly fine. When an error is raised, the subprocess.run returns a non-zero return code and my program continues as normal. What is happening? command is typically structured as a multiline list of valid terminal commands. import os import subprocess import gptinterface as ChatGPT import time initial_prompt = r””” ommited for clarity “”” workfolder = r”C:UsersRoberPycharmProjectsAI_coderworkplace” command = ChatGPT.send_msg(initial_prompt) while True: command = command.strip(“`”).strip() print(“>>> “+repr(command)+””) if command == “help”: command = ChatGPT.send_msg(initial_prompt) continue start = time.time() result = subprocess.run(str(command), cwd=workfolder, shell=True, capture_output=True, env=os.environ, text=True, timeout=5) end = time.time() print(“finished command”) if result.returncode == 0: time_taken = (end-start)*1000 print(result.stdout) command = ChatGPT.send_msg(f”Success:n{result.stdout}nnTook {time_taken:.1f} msnSend ‘help’ if you want a reminder of your target.”) else: print(result.stderr) command = ChatGPT.send_msg(“Error:n”+result.stderr+”nSend ‘help’ if you want a reminder of your target.”) submitted by /u/Entraxipy [link] [comments]