You need to enable JavaScript to run this app. 如何将Python中input()输入存储到用户可访问修改的文件实现数据持久化
优惠活动
大模型
产品
解决方案
定价
更多

如何将Python中input()输入存储到用户可访问修改的文件实现数据持久化

Python小型数据库持久化实现原理说明

现有代码参考

def end_task():
    print("\nError: Invalid input/User-Forced Shutdown has occurred.")
    print("Program shutdown.")
    exit()


def get_user_answers():
    answer = input("\n{0} Player List\n{1} Server List\n{2} Ally List\n{3} Enemy List\n{4} DNG Servers\n\n").lower()
    if answer.lower() == "zero":
        print(zero)
        repeat = input("\nWould you like to access another file? (yes\\no): ").lower()
        if repeat.lower() == 'yes':
            get_user_answers()
        else:
            print("Goodbye!")

    if answer.lower() == "one":
        print(one)
        repeat = input("\nWould you like to access another file? (yes\\no): ").lower()
        if repeat.lower() == 'yes':
            get_user_answers()
        else:
            print("Goodbye!")

    if answer.lower() == "two":
        print(two)
        repeat = input("\nWould you like to access another file? (yes\\no): ").lower()
        if repeat.lower() == 'yes':
            get_user_answers()
        else:
            print("Goodbye!")

    if answer.lower() == "three":
        print(three)
        repeat = input("\nWould you like to access another file? (yes\\no): ").lower()
        if repeat.lower() == 'yes':
            get_user_answers()
        else:
            print("Goodbye!")

    if answer.lower() == "four":
        print(four)
        repeat = input("\nWould you like to access another file? (yes\\no): ").lower()
        if repeat.lower() == 'yes':
            get_user_answers()
        else:
            print("\nGoodbye!")

    if answer.lower() != ["zero", "one", "two", "three", "four"]:
        end_task()


print("Hello! Welcome to the ---- Database! Made by \"discordname\"!")
print("Please try to answer all questions in lowercase.")
ans = input("What is the passcode: ").lower()

if ans.lower() == "----":
    zero = ["player1", "player2"]
    one = ["Server 1", "Server 2"]
    two = ["Ally 1", "Ally 2"]
    three = ["Enemy 1", "Enemy 2"]
    four = ["Server A", "Server B"]
    print("\nWelcome. Please specify what you would like to access (Answer in word form of #): ")
    get_user_answers()
else:
    end_task()

持久化实现核心逻辑

  • 你当前代码里的密码、各个列表数据都存在运行内存中,程序关闭后内存会被系统回收,所有数据就会丢失。要实现下次打开还能读取,就需要把数据写入磁盘的本地文件中,磁盘存储的内容断电、关程序都不会消失。
  • 完整的读写逻辑是:
    1. 程序启动第一步,先读取本地存储的文件内容,把内容转换成你代码需要的变量格式(比如密码字符串、各个列表)
    2. 当用户输入新内容(比如修改密码、新增玩家条目)时,先更新代码里的对应变量,再把更新后的变量重新写入本地文件覆盖旧内容
  • 新手推荐用JSON格式存储数据,Python自带json标准库,不需要额外安装,就能直接把列表、字典这类Python原生数据结构直接写入文件,也能直接从文件读出来转换成对应结构,不需要自己写解析逻辑。

常见疑问解答

1. 存到文件后需要我的电脑一直运行其他人才能访问吗?

分两种使用场景:

  • 如果你把程序打包后发给其他人,让对方在自己的电脑上本地运行:数据文件会存在对方的本地磁盘,对方随时打开程序就能读写数据,和你的电脑是否运行没有任何关系
  • 如果你是把程序部署在自己的电脑上作为服务端,其他人通过网络远程连接访问你的程序:那你的电脑需要保持运行、网络畅通,其他人才能正常访问到你电脑上存储的数据文件

2. 存储数据的文件会内置到程序本身当中吗?

默认不会。你写代码写入的文件是和你的Python脚本(或者打包后的exe程序)独立分开的磁盘文件,和程序本身是两个独立的文件。
如果你打包程序时想把初始的默认数据(比如初始密码、初始的玩家列表)内置到程序里也可以实现,但用户后续修改产生的新数据还是会存在本地磁盘的独立文件中,不会修改你打包时内置的初始内容。

现有代码的小问题提示

你当前代码里的输入合法性判断逻辑有问题:if answer.lower() != ["zero", "one", "two", "three", "four"]: 这里是字符串和列表做相等判断,永远会返回True,会导致即使用户输入了正确的选项也会触发end_task()。正确的写法应该是判断字符串是否在列表中:if answer.lower() not in ["zero", "one", "two", "three", "four"]:

内容的提问来源于stack exchange,提问作者Daniel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.16 23:06:00