python - Python - gphoto2:如何将输出转换为 JSON 或 python 数组
问题描述
我正在使用 gphoto2,大多数命令都在工作,但我不知道如何使用命令行的输出进行下一步操作。
例如:
我想获取相机中的照片列表
command = ["sudo","gphoto2","--get-all-files","--skip-existing"]
call(command)
结果:
There is no file in folder '/'.
There is no file in folder '/store_00010001'.
There is no file in folder '/store_00010001/DCIM'.
There are 6 files in folder '/store_00010001/DCIM/100EOS5D'.
#1 OM6_0018.JPG rd 12335 KB image/jpeg
#2 OM6_0019.JPG rd 12686 KB image/jpeg
#3 OM6_0020.JPG rd 12500 KB image/jpeg
#4 OM6_0021.JPG rd 12438 KB image/jpeg
#5 OM6_0022.JPG rd 12668 KB image/jpeg
#6 OM6_0023.JPG rd 12760 KB image/jpeg
我的目标是在 JSON 或 Python 数组中获得输出:
['OM6_0018.JPG', 'OM6_0019.JPG', 'OM6_0020.JPG', ...]
任何建议表示赞赏。
解决方案
所以,我不知道 gphoto 是如何工作的,但我会大胆猜测,而不是进行子进程调用,你应该这样做:
import gphoto2 as gp
def list_files(camera, path='/'):
result = []
# get files
for name, value in gp.check_result(
gp.gp_camera_folder_list_files(camera, path)):
result.append(os.path.join(path, name))
# read folders
folders = []
for name, value in gp.check_result(
gp.gp_camera_folder_list_folders(camera, path)):
folders.append(name)
# recurse over subfolders
for name in folders:
result.extend(list_files(camera, os.path.join(path, name)))
return result
camera = ... #TODO set camera here
path = ... #TODO set path here
files = list_files(camera, path)
有关完整示例,请参见此处:https ://github.com/jim-easterbrook/python-gphoto2/blob/master/examples/list-files.py
推荐阅读
- javascript - 元素转换时不会触发 Mouseleave 事件
- sql-server - 在 TSQL 中生成等效的脚本
- c++ - 联合声明的两种不同语法如何给我不同的大小?
- enums - 在 OpenAPI (Swagger) 中重用枚举的子集
- maven - 可以从命令行将 maven updatePolicy 设置为从不吗?
- python - 如何将 60 分钟的数据点划分为 15 分钟?
- google-apps-script - 优化 OnEdit() 脚本
- android - Cordova 应用程序仅在分屏视图/多个应用程序视图中显示
- mysql - 如何使用游标根据另一列更新某些列?
- c - 如何将 char** 转换为 Vec
生锈