0

im making a blob extractor to get some images from a db with a query for the following rows: rows selected from query

so im using the following code to extract said images:

import psycopg2
import base64

# Conectar a la base de datos
conn = psycopg2.connect(user="serverinfo") 

cursor = conn.cursor()
# Ejecutar la consulta
cursor.execute('Select "id", "name", "flip_image" from product_template where flip_image is not null')
rows = cursor.fetchall()

for row in rows:
        product_id = row[0]
        product_name = row[1]
        image_data = row[2]
                
        if image_data:
                # Decodificar la imagen base64
                image_bytes = base64.b64decode(image_data)
                
                #Guardar la imagen en un archivo
                with open(f"{product_id}.png", 'wb') as image_file:
                        image_file.write(image_bytes)
                print(f"imagen {product_name} (ID: {product_id})guardada.")
                
cursor.close()
conn.close()

as it is right now the code is working and i get all the image files as long as i have

with open(f"{product_id}.png", 'wb') as image_file:

but when i try to change the product_id to product_name to print all the pngs with the actual name of the image instead of an id using

with open(f"{product_name}.png", 'wb') as image_file:

or

with open(f"{product_id}_{product_name}.png", 'wb') as image_file:

i get a no file error:

FileNotFoundError: [Errno 2] No such file or directory: 'Caja Registradora Casio Pcr-t280 / 1200 Items Con Nombre.png'

Why would the extraction work only with the product id variable and not the product name variable?

2
  • There is a slash in the product name. That confounds open() because it looks for a directory called Caja Registradora Casio Pcr-t280 Commented Jun 20 at 15:35
  • Thanks, i didnt saw this, what would be the proper way to bypass the slash in open function? i bypassed it for now just getting rid of the "/" in the products because they were redundant in most cases.
    – nando-ando
    Commented Jun 24 at 19:09

0

Browse other questions tagged or ask your own question.