简体   繁体   English

Python-gphoto2:如何将输出转换为JSON或python数组

[英]Python - gphoto2: how to convert output to JSON or python array

I am working with gphoto2, most commands are working, but I don't know how to use the output from command-line for next operations. 我正在使用gphoto2,大多数命令都在工作,但是我不知道如何使用命令行的输出进行下一步操作。

For example: 例如:

I want to get list of photos in camera 我想获取相机中的照片列表

command = ["sudo","gphoto2","--get-all-files","--skip-existing"]
call(command)

Result: 结果:

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

What I aim to get output in JSON or Python array: 我想要在JSON或Python数组中输出的内容:

['OM6_0018.JPG', 'OM6_0019.JPG', 'OM6_0020.JPG', ...]

Any suggestion is appreciated. 任何建议表示赞赏。

So, I have no idea how gphoto works, but I am going to take a wild guess that instead of doing a subprocess call, you should instead just do this: 因此,我不知道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)

See here for full example: https://github.com/jim-easterbrook/python-gphoto2/blob/master/examples/list-files.py 完整示例参见此处: https : //github.com/jim-easterbrook/python-gphoto2/blob/master/examples/list-files.py

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

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