gpt4 book ai didi

python - 如何保存 session 数据(Python)

转载 作者:行者123 更新时间:2023-11-28 18:47:06 25 4
gpt4 key购买 nike

假设我写了一些代码,其他人使用并输入了他们的名字,并做了代码中包含的其他事情。

name = input('What is your name? ')
money = 5
#There is more stuff, but this is just an example.

我如何才能将该信息保存到一个文本文件中,以便稍后调用以继续该 session 。就像视频游戏中的保存点。

最佳答案

您可以将信息写入文本文件。例如:

我的文本.txt

(empty)

我的文件.py
name = input('What is your name? ')
...
with open('mytext.txt', 'w') as mytextfile:
mytextfile.write(name)
# Now mytext.txt will contain the name.

然后再次访问它:

with open('mytext.txt') as mytextfile:
the_original_name = mytextfile.read()

print the_original_name
# Whatever name was inputted will be printed.

关于python - 如何保存 session 数据(Python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18307968/

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