Need to assign the result of a command line that runs another .py file to a Variable in Python, but modify the command as needed within the code
Question:
I need to assign the result of the following command line to a variable:
!python script.py –img_file ../data/img.png
The catch is that I need to run this within a for loop and change the path "../data/img.png" to "../data/img2.png", "../data/img3.png", etc. based on the loops.
A snippet of the code that does not work but shows the idea:
for i in range(len(uppers)):
cv2.imwrite('data/img' + str(img_num) + '.png', img[uppers[i]:lowers[i],:])
img_var = !python script.py --img_file ../data/img+str(img_num).png
img_num += 1
Appreciate any help or thoughts.
Answers:
You can achieve this by building the command as a string and passing it to the subprocess module’s run() method. Here’s an example:
import subprocess
img_num = 1
for i in range(len(uppers)):
cv2.imwrite('data/img' + str(img_num) + '.png', img[uppers[i]:lowers[i],:])
img_path = '../data/img' + str(img_num) + '.png'
command = ['python', 'script.py', '--img_file', img_path]
result = subprocess.run(command, capture_output=True, text=True)
img_var = result.stdout
img_num += 1
We pass the capture_output=True
parameter to capture both the standard output and the standard error streams, and the text=True
parameter to get the output as a string instead of bytes.
The img_path
variable is created inside the loop to update the image file path based on the loop counter. The img_var
variable will contain the output of the script for each loop iteration. You can then process this output as needed.
I need to assign the result of the following command line to a variable:
!python script.py –img_file ../data/img.png
The catch is that I need to run this within a for loop and change the path "../data/img.png" to "../data/img2.png", "../data/img3.png", etc. based on the loops.
A snippet of the code that does not work but shows the idea:
for i in range(len(uppers)):
cv2.imwrite('data/img' + str(img_num) + '.png', img[uppers[i]:lowers[i],:])
img_var = !python script.py --img_file ../data/img+str(img_num).png
img_num += 1
Appreciate any help or thoughts.
You can achieve this by building the command as a string and passing it to the subprocess module’s run() method. Here’s an example:
import subprocess
img_num = 1
for i in range(len(uppers)):
cv2.imwrite('data/img' + str(img_num) + '.png', img[uppers[i]:lowers[i],:])
img_path = '../data/img' + str(img_num) + '.png'
command = ['python', 'script.py', '--img_file', img_path]
result = subprocess.run(command, capture_output=True, text=True)
img_var = result.stdout
img_num += 1
We pass the capture_output=True
parameter to capture both the standard output and the standard error streams, and the text=True
parameter to get the output as a string instead of bytes.
The img_path
variable is created inside the loop to update the image file path based on the loop counter. The img_var
variable will contain the output of the script for each loop iteration. You can then process this output as needed.