![](/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.