简体   繁体   English

Python/Docker: FileNotFoundError: [Errno 2] 没有这样的文件或目录:

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

相关问题 python:FileNotFoundError:[Errno 2]没有这样的文件或目录 - python: FileNotFoundError: [Errno 2] No such file or directory Python FileNotFoundError:[错误2]没有这样的文件或目录 - Python FileNotFoundError: [Errno 2] No such file or directory Python FileNotFoundError: [Errno 2] 没有这样的文件或目录: - Python FileNotFoundError: [Errno 2] No such file or directory: Python 3-FileNotFoundError:[Errno 2]没有这样的文件或目录 - Python 3 - FileNotFoundError: [Errno 2] No such file or directory Python 3.5:FileNotFoundError:[Errno 2]没有这样的文件或目录 - Python 3.5: FileNotFoundError: [Errno 2] No such file or directory Python 错误 FileNotFoundError: [Errno 2] 没有这样的文件或目录 - Python Error FileNotFoundError: [Errno 2] No such file or directory 我得到了 Python: FileNotFoundError: [Errno 2] No such file or directory - i got Python: FileNotFoundError: [Errno 2] No such file or directory Python FileNotFoundError: [Errno 2] No such file or directory: Synology 上的“ffprobe” - Python FileNotFoundError: [Errno 2] No such file or directory: 'ffprobe' on Synology FileNotFoundError: [Errno 2] 没有这样的文件或目录 Azure Python 错误 - FileNotFoundError: [Errno 2] No such file or directory Azure Python error Python 错误:FileNotFoundError: [Errno 2] 没有那个文件或目录 - Python error: FileNotFoundError: [Errno 2] No such file or directory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM