简体   繁体   English

如何检索使用从 python 脚本调用的 shell 脚本创建的文件名和文件?

[英]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.我有一个调用 shell 脚本的 python 脚本。 The shell script captures an image, names the image using a timestamp, and then saves the image to directory. shell 脚本捕获图像,使用时间戳命名图像,然后将图像保存到目录。 When the shell script finishes, I would like to access the image from the python script that called the shell script.当 shell 脚本完成时,我想从调用 shell 脚本的 python 脚本访问图像。

Here is my shell script capture.sh :这是我的 shell 脚本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 :这个 shell 脚本是从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.您可以将 bash 脚本创建的文件的名称发送到 standardout 并在 python 脚本中捕获该文件名。 Here is a similar SO question that I think will lead you in the right direction.这是一个类似的 SO question,我认为它将引导您朝着正确的方向前进。

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 .正如该问题的最佳答案所指出的那样,这将取决于您正在使用的 python 版本。看起来您正在使用subprocess.call In terms of the current python docs for subprocess , that is the old API call.就 subprocess 的当前subprocess文档而言,即旧的 API 调用。 You can now use subprocess.run in versions 3.5 and newer, which is the recommended way to invoke child processes.您现在可以在 3.5 及更高版本中使用subprocess.run ,这是调用子进程的推荐方式。

Running shell command and capturing the output 运行 shell 命令并捕获 output

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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