簡體   English   中英

如何刪除 docker 容器內 python 中的文件?

[英]How to delete file in python inside docker container?

下面的代碼是我的代碼的一部分。 在我的本地機器上,一切都很好。但是,我部署了我的代碼並在 docker 容器內部,它給出了錯誤"result": "[Errno 13] Permission denied: path" 還有什么可以在 docker 容器中刪除的解決方案? 我也試過os.remove() ,它沒有用。

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}") 

我不知道這個文件有什么問題以及如何刪除它

但我會使用io.BytesIO在 memory 中創建文件,然后它不會在磁盤上創建文件,也不需要刪除它

我沒有pisa來測試它,但它應該是這樣的

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())

模塊io是標准模塊,因此您不必安裝它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM