[英]How to delete file in python inside docker container?
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" .在我的本地机器上,一切都很好。但是,我部署了我的代码并在 docker 容器内部,它给出了错误"result": "[Errno 13] Permission denied: path" 。 What could be solution to delete in docker container also?还有什么可以在 docker 容器中删除的解决方案? I tried os.remove()
also,It didn't work.我也试过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}")
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但我会使用io.BytesIO
在 memory 中创建文件,然后它不会在磁盘上创建文件,也不需要删除它
I don't have pisa
to test it but it should be something like this我没有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())
Module io
is standard module so you don't have to install it.模块io
是标准模块,因此您不必安装它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.