![](/img/trans.png)
[英]python: FileNotFoundError: [Errno 2] No such file or directory
[英]Python/Docker : FileNotFoundError: [Errno 2] No such file or directory:
我正在使用 Python 代码打开一个文本文件并写入一些信息并关闭它。 当我在 Jupyter 笔记本上运行此代码时,它运行良好,但是当我将其作为 Docker 容器的一部分运行时,它会出现以下错误。
当前目录是 C:/app 我存储了 Dockerfile、testfile.txt 和 Hello1.py 的位置。 另外我去了虚拟机并添加了 C: 作为共享文件夹。
Python 文件
file = open("C:/Python/testfile.txt","w")
file.write("Hello World")
file.write("This is our new text file")
file.close()
Docker 文件
FROM python:latest
WORKDIR /data
COPY testfile.txt /data
COPY Hello1.py /data
CMD ["python","Hello1.py"]
收到错误
$ 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'
您的 python 程序无法访问主机的C:/
。 您需要更改文件路径以引用容器中存在的testfile.txt
。
file = open("/data/testfile.txt","w")
另请注意,这不会修改主机上存在的testfile.txt
。 它将写入容器内的文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.