python—写入.txt文件很慢,而且不是即时的

xoshrz7s  于 2021-08-20  发布在  Java
关注(0)|答案(0)|浏览(238)

**已关闭。**此问题需要调试详细信息。它目前不接受答案。
**想要改进此问题?**更新问题,使其位于堆栈溢出主题上。

昨天关门了。
改进这个问题
我试图将一些变量的数据写入.txt文件
但不知何故,它不会立即写入,我需要等待大约3分钟才能写入文件(.txt文件大小持续增加3分钟)。
但是,如果没有将其写入文件的代码,结果将立即打印到控制台。这意味着我的代码没有问题,而是编写命令代码。

def generate_episode(policy):

# let's define a list called episode for storing the episode

episode = []

# initialize the state by resetting the environment

state = env.reset()

file = open("generate_policy.txt", "a")
file.write("\n\n\n ::::: Initial state = " + str(state) + " :::::\n" )

# then for each time step

for i in range(num_timestep):

    #select the action according to the given policy
    action = policy(state) # Policy(state) will return 0 if state[0] > 19 and otherwise

    #perform the action and store the next state information, take next step with action 'action' value variable
    next_state, reward, done, info = env.step(action)

    #store the state, action, reward into our episode list
    episode.append((state, action, reward))

    file.write("\n " + str(i) + " iteration\n" )
    file.write("\n Action               = " + str(action) + " \n " +"Next State = "+ str(next_state)  )
    file.write("\n Reward            = "+ str(reward)  )

    file.write("\n")  

    #If the next state is a final state then break the loop else update the next state to the current state
    if done:
        file.write("\n Done            = "+ str(done)  )
        file.close
        break

    state = next_state

return episode

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题