0

The code below is part of my code. In my local machine, everything is fine.However, I deploy my code and inside of docker container,it gives the error "result": "[Errno 13] Permission denied: path". What could be solution to delete in docker container also? I tried os.remove() also,It didn't work.

path = "/mypath/"
output = path + "myfile.pdf"

result_file = open(output, "w+b")

pisa_res = pisa.CreatePDF(
        source_html,               
        dest = result_file)

result_file.close()  

with open(output, "rb") as pdf_file:
    encoded_string = base64.b64encode(pdf_file.read())       

os.system(f"rm -rf {output}") 

Elvin Jafarov
  • 1,341
  • 11
  • 25
  • Check if the user that executes this code has rights to delete this file. Sounds like a permissions issue. – Kroustou Dec 18 '20 at 12:29
  • This might help, also, I would recommend attaching a tmpfs to the container or your /tmp folder, that will be easier to manage. https://stackoverflow.com/questions/42816048/docker-temporary-files-strategy/55104489 – Emin Mastizada Dec 18 '20 at 12:33
  • I don't understand why you save it in file and later read it -- can't you do it withoute file ? Eventually you can use `io.BytesIO` to create file in memory without writing on disk. And then you don't have to delete it. – furas Dec 18 '20 at 12:58
  • @EminMastizada it is not what I want. – Elvin Jafarov Dec 18 '20 at 13:06
  • @Kroustou I will check it – Elvin Jafarov Dec 18 '20 at 13:06

1 Answers1

2

I don't know what is the problem with this file and how to delete it

but I would use io.BytesIO to create file in memory and then it doesn't create file on disk and it doesn't need to delete it

I don't have pisa to test it but it should be something like this

import io

result_file = io.BytesIO()

pisa_res = pisa.CreatePDF(
        source_html,               
        dest=result_file)

result_file.seek(0) # move to the beginning of file to read it

encoded_string = base64.b64encode(result_file.read())

Module io is standard module so you don't have to install it.

furas
  • 134,197
  • 12
  • 106
  • 148