aGiral
aGiral

Reputation: 33

Exiftool export JSON with Python

I´m trying to extract some metadata and store them in a JSON file using Exiftool via Python.

If I run the following command (according to the documentation) in the CMD it works fine, generating a temp.json file:

exiftool -filename -createdate -json C:/Users/XXX/Desktop/test_folder > C:/Users/XXX/Desktop/test_folder/temp.json

When managing Exiftool from Python the data is extracted correctly but no JSON file is generated.

import os
import subprocess

root_path = 'C:/Users/XXX/Desktop/test_folder'

for path, dirs, files in os.walk(root_path):
    for file in files:
        file_path = path + os.sep + file
        exiftool_exe = 'C/Users/XXX/Desktop/exiftool.exe'
        json_path = path + os.sep + 'temp.json'
        export = os.path.join(path + ' > ' + json_path)

        exiftool_command = [exiftool_exe, '-filename', '-createdate', '-json', export]
        process = subprocess.run(exiftool_command)
        print(process.stdout)

When I run the code it shows the error:

Error: File not found - C:/Users/XXX/Desktop/test_folder > C:/Users/XXX/Desktop/test_folder/temp.json  

What am I missing, any ideas on how to get it to work? Thanks!

Edit with the solution:

I let the fixed code here just in case it could help someone else:

import os
import subprocess

root_path = 'C:/Users/XXX/Desktop/test_folder'

for path, dirs, files in os.walk(root_path):
    for file in files:
        file_path = path + os.sep + file
        exiftool_exe = 'C/Users/XXX/Desktop/exiftool.exe'
        export = root_path + os.sep + 'temp.json'

        exiftool_command = [exiftool_exe, file_path, '-filename', '-createdate', '-json', '-W+!', export]
        process = subprocess.run(exiftool_command)
        
        print(process.stdout)

Thanks to StarGeek!

Upvotes: 3

Views: 1301

Answers (1)

StarGeek
StarGeek

Reputation: 5791

I believe the problem is that file redirection is a property of the command line and isn't available with subprocess.run. See this StackOverflow question.

For a exiftool solution, you would use the -W (-tagOut) option, specifically -W+! C:/Users/XXX/Desktop/test_folder/temp.json. See Note #3 under that link.

Upvotes: 2

Related Questions