简体   繁体   中英

How to retrieve a filename and file created using a shell script that was called from a python script?

I have a python script that calls a shell script. The shell script captures an image, names the image using a timestamp, and then saves the image to directory. When the shell script finishes, I would like to access the image from the python script that called the shell script.

Here is my shell script capture.sh :

DATE=$(date +"%Y-%m-%d_%H%M%S")
fswebcam -r 1280x720 --no-banner /home/pi/app/images/$DATE.jpg
exit 0

This shell script is called from capture.py :

import os
import subprocess

# call script
subprocess.call(['/home/pi/app/capture.sh'])

#?how to retrieve and process image: /home/pi/app/images/$DATE.jpg

Any advice would be welcome! Thank you!

You can send the name of the file created by the bash script to standardout and capture that in the python script. Here is a similar SO question that I think will lead you in the right direction.

As the top answer to this question points out, it will depend on what version of python you're doing this work in. It looks like you're using subprocess.call . In terms of the current python docs for subprocess , that is the old API call. You can now use subprocess.run in versions 3.5 and newer, which is the recommended way to invoke child processes.

Running shell command and capturing the output

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM