简体   繁体   English

如何删除 docker 容器内 python 中的文件?

[英]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.

相关问题 如何以编程方式使用 python 从 docker 容器卷中删除文件? - How to delete a file from docker container volume with python programmatically? 如何在 expressjs Docker 容器中运行 Python - How to run Python inside an expressjs Docker container Docker 中的 Python 日志记录。 如何访问Docker容器内部形成的日志文件 - Python logging in Docker. How to access log file formed inside the Docker container 如何在python中从docker容器获取文件 - How get file from docker container in python "如何使用在 Docker 容器中运行的 Python API 程序创建正确的文件路径?" - How create a correct file path with a Python API program that is running inside a Docker Container? 无法使用 Python 在 docker 容器内创建文件 SDK - Unable to create file inside docker container using Python SDK 容器内的 Python Docker SDK - Python Docker SDK inside container 如何在Docker容器内使用python子进程执行mysql命令 - How to perform mysql command using python subprocess inside docker container 如何在 docker 容器中安装 Python-dev? - How can i install Python-dev inside a docker container? 如何将参数传递给在 docker 容器内运行的 python 脚本? - How to pass arguments to the python script that is running inside docker container?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM