简体   繁体   English

如何同时在Python输入中追加和覆盖文件

[英]How to append and overwrite at the same time a file from an input in Python

i'm trying to append and overwrite at the same time using the following code: 我正在尝试使用以下代码同时添加和覆盖:

with open("file.txt",'r') as f:
     data=f.readlines()
     user_amount = data[2].rstrip()

def deposit(event):
    if event >= 0:
        user_amount += event
    with open("file.txt", 'a') as a:
        a.writelines(event)

def save():
    with open("file.txt",'w') as f:
         for i,line in enumerate(data,1):             
             if i == 3:                                          
                f.writelines(user_amount)
             else:
                f.writelines(line)

the file is composed by the following lines: 该文件由以下几行组成:

 first line
 second line
 user_amount
 fourth line

So, basically what I want to do is: I have a button called "Deposit" that when I press it call the function deposit() that get the values from an Entry and save in the variable user_amount. 因此,基本上我想做的是:我有一个名为“ Deposit”的按钮,当我按下它时,调用函数deposit(),该函数从Entry中获取值并将其保存在变量user_amount中。 For example, if I put 10 in the Entry and press the button Deposit the user_amount sum 10 and the value is showed in a Label. 例如,如果我在“条目”中输入10,然后按“存入user_amount之和”按钮10,则该值将显示在“标签”中。 So far is fine, the values are being appended to the file every time that I press the button deposit. 到目前为止还好,每次我按按钮存款时,值都会附加到文件中。 However, there is another button called log_out that I need to press and call the function save() to save the "new amount" at the index 2! 但是,还有一个名为log_out的按钮,我需要按下该按钮并调用函数save()以将“新金额”保存在索引2中! Works fine the index 2 in my file is updated to the new amount, but my problem is being that all appends are disappearing. 工作正常,我文件中的索引2已更新为新的数量,但是我的问题是所有附加项都消失了。 being more clear, I need appending all values inputted by the Entry at the end of the file, and when I press the button log_out I need to save the new amount and keep all append made. 更清楚地说,我需要在文件末尾附加所有由Entry输入的值,当我按下log_out按钮时,我需要保存新的金额并保留所有附加内容。 The first, second and fourth line I can't modify. 我无法修改的第一,第二和第四行。 the output should be: 输出应为:

first_line
second_line
user_amount (new_amount)
fourth_line
Deposit
100.0
Deposit
50.0
Deposit
200.0... line by line ( will show all deposits made)

EDIT: my code probably won't work properly if event is an integer, but then the code you show wouldn't have produced the output you show, specifically you don't show anything that adds the "Deposit" line in the output you show. 编辑:如果event是一个整数,我的代码可能无法正常工作,但是您显示的代码将不会产生您显示的输出,特别是您不会显示任何在您的输出中添加“存款”行的内容节目。 That's why StackOverflow asks you to show an Minimal Complete Verifiable Example https://stackoverflow.com/help/mcve when you want help with code. 因此,当您需要代码帮助时,StackOverflow要求您显示一个最小的完整可验证示例https://stackoverflow.com/help/mcve

The problem (I think) is that you are keeping a copy of the original file in variable data , and you aren't updating that when you call the deposit function, so when you call the save function (which rewrites the whole file) the content of variable data is written out and therefore the deposits are lost 问题(我认为)是您在可变data中保留了原始文件的副本,并且在调用deposit函数时没有更新该文件,因此当您调用save函数(重写整个文件)时,可变data内容被写出,因此损失了存款

You might be able to fix it by updating data in the deposit function like this (untested) (I am assuming event is a list): 您可以通过像这样(未测试)更新存款功能中的数据来解决此问题(我假设event是一个列表):

def deposit(event):
    if event >= 0:
        user_amount += event
    with open("file.txt", 'a') as a:
        a.writelines(event)
    data.extend(event)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM