[英]Python/Docker : FileNotFoundError: [Errno 2] No such file or directory:
I am using a Python code to open a text file and write some information and close it.我正在使用 Python 代码打开一个文本文件并写入一些信息并关闭它。 When I run this code on Jupyter notebook it runs perfectly but when I run this as a part of Docker container it gives the following error.
当我在 Jupyter 笔记本上运行此代码时,它运行良好,但是当我将其作为 Docker 容器的一部分运行时,它会出现以下错误。
Current directory is C:/app where I have stored Dockerfile, testfile.txt and Hello1.py.当前目录是 C:/app 我存储了 Dockerfile、testfile.txt 和 Hello1.py 的位置。 In addition I have gone to Virtual Machine and have added C: as shared folder.
另外我去了虚拟机并添加了 C: 作为共享文件夹。
Python File Python 文件
file = open("C:/Python/testfile.txt","w")
file.write("Hello World")
file.write("This is our new text file")
file.close()
Docker File Docker 文件
FROM python:latest
WORKDIR /data
COPY testfile.txt /data
COPY Hello1.py /data
CMD ["python","Hello1.py"]
Error Recieved收到错误
$ docker run sid1980
Traceback (most recent call last):
File "Hello1.py", line 7, in <module>
file = open("C:/Python/testfile.txt","w")
FileNotFoundError: [Errno 2] No such file or directory: 'C:/Python/testfile.txt'
Your python program cannot access C:/
of the host machine.您的 python 程序无法访问主机的
C:/
。 You need to change the file path to reference the testfile.txt
that exists within the container .您需要更改文件路径以引用容器中存在的
testfile.txt
。
file = open("/data/testfile.txt","w")
Also note that this will not modify the testfile.txt
that exists on the host.另请注意,这不会修改主机上存在的
testfile.txt
。 It will write to the file that is inside the container.它将写入容器内的文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.