Hello, I’m a newbie with streamlit and in the programation industry, I’m doing a page with an st.file_uploader()
in a form, that, with the help of a with NamedTemporaryFile(dir = '.\uploaded_images', suffix =' .jpg', delete = False, delete_on_close = True) as tmp:
is able to send multiple images to a folder in google drive.
It works good when its about uploading the images to the folder in Google Drive, but when I want to erase the temp files, jumps me an error that says “[[WinError 32] The process cannot access the file because it is being used by another process]”.
Thus, I research for a solution to this problem and I find a several post about this error but any of those couldn’t fix the problem.
I tried use tmp.close()
inside and outside the with
block and nothing changes.
I tried make a list[temp.name]
that contains the path of the tempfiles that were sent and using it out of the with
block:
while i < len(aux):
if i+1 == len(aux):
i += 1
st.stop
else:
os.remove(aux[i])
i += 1
With that method I’m able to erase all the images except the last image sent, so technically it doesn’t work, but hey, at least you are not going to have 50 tempfiles, it’s only one hahaha. But the idea is don’t leave trash files in the computer ,so the problem still
Please I literally have been all the day trying to figure out how can i resolve this problem. HELP!!
The part of the code in question:
if enviar:
if (nombre_bien == "" or porque_chatarra == "" or valor_chatarra == ""
or patio == "" or area == "" or puesto == ""
or fecha == "" or hora == "" or len(archivo_imagen) == 0):
st.error("POR FAVOR RELLENAR TODOS LOS CAMPOS")
elif len(archivo_imagen) == 1:
st.error("POR FAVOR INSERTAR UNA IMAGEN ADICIONAL, SON 2 COMO MINIMO")
else:
i = 0
aux = []
uid = generate_uid()
id = google.ID()
value =[[id, str(fecha), str(hora), nombre_bien, porque_chatarra, str(valor_chatarra), patio, area, puesto, uid]]
range = google.get_last_row_range()
google.write_data(range,value)
while i < len(archivo_imagen):
if archivo_imagen[i].type == "image/png":
sufijo = '.png'
elif archivo_imagen[i].type == "image/jpeg":
sufijo = '.jpeg'
elif archivo_imagen[i].type == "image/jpg":
sufijo = '.jpg'
with tempfile.NamedTemporaryFile(dir='.\imagenes subidas', suffix=sufijo,delete=False,delete_on_close=True) as tmp:
archivo_imagen[i].name = f"{id}_{nombre_bien}_{fecha}_{i}"
tmp.write(archivo_imagen[i].getvalue())
credenciales = gd.login()
gfile = credenciales.CreateFile({'parents': [{'id': carpeta}], "title": archivo_imagen[i].name})
gfile.SetContentFile(tmp.name)
gfile.Upload()
aux.append(tmp.name)
i += 1
tmp.close()
# --------------------- Remover archivos y Finalizar ---------------------------------------------
i = 0
while i < len(aux):
if i+1 == len(aux):
i += 1
st.stop
else:
os.remove(aux[i])
i += 1
st.success("Se ha completado el registro exitosamente")