gpt4 book ai didi

python - 为子进程写入文件输入

转载 作者:行者123 更新时间:2023-11-28 16:27:36 26 4
gpt4 key购买 nike

我需要在循环中调用一个例程,该例程将文本文件作为输入。因为我不想一直打开和关闭文本文件,所以我在循环中保持打开状态。例如:

with open("test.txt",'r+') as w_file:

w_file.write(str(0.8) + "\n" + str(0.2))
subprocess.call(["cat","test.txt"]) #Here I want to call my routine

但文件仍处于旧状态。为什么?处理这个问题的最佳方法是什么?

最佳答案

写入后需要关闭文件。尝试:

with open("test.txt",'r+') as w_file:
w_file.write(str(0.8) + "\n" + str(0.2))

subprocess.call(["cat","test.txt"]) #Here I want to call my routine

或者不关闭,刷新文件

with open("test.txt",'r+') as w_file:
w_file.write(str(0.8) + "\n" + str(0.2))
w_file.flush()
subprocess.call(["cat","test.txt"]) #Here I want to call my routine

关于python - 为子进程写入文件输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35179756/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com