Docker无法将文本文件构建为输出文件,也无法搜索该文件。

2024-09-27 07:29:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个python脚本,它正在创建一个文本文件。当我单独运行python脚本时,它运行得很好。在

class TXT_file():
### initialize the txt_file class
def __init__(self, path2file):
    self.path2file = path2file     
    with open(path2file, "w") as f: 
        f.write("") 

### write sentence to the txt_file
def sentence_to_file(self, sentence):
    with open(self.path2file, "w") as f: 
        f.write(sentence) 

### function initiating the class file and writing "Hello world!"
def run_sequence(path2data, file):
    ### initialize the gps_file class
    my_txt_file = TXT_file(path2data + file)
    my_txt_file.sentence_to_file("Hello world!")

### main
if __name__ == "__main__":
    ### setting up paths to various data files
    path2data = 'C:\\Users\\abc\\folder\\'
    file = "example_text_file.txt"
    run_sequence(path2data, file)

当我用它建立一个码头工人形象。我已经为windows安装了一个Docker并使用windows容器。Dockerfile看起来像

^{pr2}$

我无法将文件example_text_file.txt作为输出,它引发一个错误:

我使用以下命令来构建和运行docker映像:

docker build -t test_print_1 .
docker run test_print_1

我得到的错误是:

Traceback (most recent call last):
  File "./txt_file_creation.py", line 34, in <module>
    run_sequence(path2data, file)
  File "./txt_file_creation.py", line 26, in run_sequence
    my_txt_file = TXT_file(path2data + file)
  File "./txt_file_creation.py", line 15, in __init__
    with open(path2file, "w") as f:
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\abc\\folder\\example_text_file.txt'

如何用docker打印同一文件夹中的文件。在


Tags: thetodockerrunselftxtdefwith

热门问题