当在txt文件中插入文本时,会出现'错误'。

我有两个txt文件。这些文件是自动创建的,并且它也将’10’作为一个文本插入到这些文件中。但如果程序读取这些文件,它给出了一个错误,如 “ValueError: invalid literal for int() with base 10: ””。我怎样才能解决这个错误?

    income_txta = open(("monthly_income,expences_amount\\income\\" + year + month + ".txt"), "a")
    income_txtr = open(("monthly_income,expences_amount\\income\\" + year + month + ".txt"), "r")
    if income_txtr.read() == '':
        income_txta.write("10")
        total_income=10
    else:
        total_income = int(income_txtr.read())

    expences_txta = open(("monthly_income,expences_amount\\expences\\" + year + month + ".txt"), "a")
    expences_txtr = open(("monthly_income,expences_amount\\expences\\" + year + month + ".txt"), "r")
    if expences_txtr.read() == '':
        expences_txta.write("10")
        total_expences = 10
    else:
        total_expences = int(expences_txtr.read())
        print(total_expences)

解决方案:

我修改了这样的代码,结果成功了。

    if os.path.exists("monthly_income,expences_amount\\income\\" + year + month + ".txt"):
        print("total_income_file exists")
    else:
        fh = open("monthly_income,expences_amount\\income\\" + year + month + ".txt", "w")

    if os.path.exists("monthly_income,expences_amount\\expences\\" + year + month + 
         ".txt"):
        print("total_expences_file exists")
    else:
                income_txtr = open(("monthly_income,expences_amount\\income\\" + year + month + ".txt"), "r")
    if income_txtr.read() == '':
        income_txta = open(("monthly_income,expences_amount\\income\\" + year + month + ".txt"), "a")
        income_txta.write("10")
        total_income=10
    else:
        income_txtr = open(("monthly_income,expences_amount\\income\\" + year + month + ".txt"), "r")
        total_income = int(income_txtr.read())


    expences_txtr = open(("monthly_income,expences_amount\\expences\\" + year + month + ".txt"), "r")
    if expences_txtr.read() == '':
        expences_txta = open(("monthly_income,expences_amount\\expences\\" + year + month + ".txt"), "a")
        expences_txta.write("10")
        total_expences = 10
    else:
        expences_txtr = open(("monthly_income,expences_amount\\expences\\" + year + month + ".txt"), "r")
        total_expences = int(expences_txtr.read())
        print(total_expences)fh = open("monthly_income,expences_amount\\expences\\" + year + month + ".txt", 
          "w")

给TA打赏
共{{data.count}}人
人已打赏
未分类

如何在Vuetify中对不同的屏幕尺寸使用断点?

2022-9-8 21:49:40

未分类

正确使用结构常量(使用动态值)。

2022-9-8 22:00:36

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索