【问题标题】:How to delete printed line of file?如何删除打印的文件行?
【发布时间】:2022-01-26 13:46:24
【问题描述】:

我的程序从kody.txt 文件中读取并在不和谐上打印它作为重播命令,我想从kody.txt 中删除打印的代码。这个怎么做?有任何想法吗? atm 代码不起作用,因为 read_coupon() 返回空值(清除所有 txt 文件)


    @client.command()
    async def kody (ctx, amount):
        await ctx.send(read_coupon(int(amount)))

def read_coupon(amount):
    result = ''
    with open("kody.txt","r") as f:
        for line in f:
            result += line
            amount -= 1
            if line == result:
                line.replace(result," ")   

            if amount == 0: break            
    
    return result
kody.txt:

NLS8JS4L4J
NLZP8PPBQ2
NLXSTD7X6S
NLQ4PW7N4B
NLNR8EEAKA
NL3WXLQUS4
NLGCCHJ9Y9
NLW9ZT4UG7
NL26PKQYZH
NLNZBFK99S

【问题讨论】:

    标签: python discord.py


    【解决方案1】:

    首先读取文件,然后将 x 行添加到结果中,并将其他行写回文件。

    def read_coupon(amount):
        result = ''
        with open("kody.txt","r") as f:
            lines = f.readlines()
        with open("kody.txt","w") as f:
            for line in lines:
                if amount!=0:
                    result+=line
                    amount-=1
                else:
                    f.write(line)
        
        return result
    

    【讨论】:

      猜你喜欢
      • 2013-01-18
      • 1970-01-01
      • 1970-01-01
      • 2011-05-27
      • 1970-01-01
      • 2011-05-28
      • 1970-01-01
      • 2018-11-20
      相关资源
      最近更新 更多