I have a question about using Python subprocess.

Asked 2 years ago, Updated 2 years ago, 98 views

As far as I know, the subprocess runs another py file, but when I actually run A1.py, only the B1.py file opens and "Hello" does not print out on A1's screen.

I went to the console and tried putting python in front of it because python B1.py is possible, but it doesn't work"T" filepath = "C:/python B1.py"

What I want is to run B1.py using subprocess (running another py file) when I run on A1.py, so I would appreciate it if you could share related information or methods.

A1.py

import subprocess

filepath = "C:/B1.py"
aaa = subprocess.check_output(filepath, shell=True)
print(aaa)

B1.py

print("Hello")

python subprocess

2022-09-22 08:06

1 Answers

The subprocess receives the command line command directly The args of check_output must be entered in the form of an execution command.

import subprocess

filename = 'subproc.py'

# It should be in the form of $python subproc.py
stdout = subprocess.check_output(["python", filename])

print(stdout)

My environment is MacOS, so it's not like Windows It would also be possible to make subproc.py executable.

Well... it's a function that receives arbitrary commands, so it can be in various forms.


2022-09-22 08:06

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.