如何打开文件并删除第一项(索引0处的项目)

问题描述 投票:0回答:1

Atm我正在为Twitch的Chat bot工作。到目前为止,我有这个工作。这样我就能将项目添加到文件中。

# Variables
f = open("Tank_request_list.txt","a+")
fr = open("Tank_request_list.txt","r")
tr = "EBR" # test input
tank_request = fr.read()
treq = tank_request.split("#")

with open("Tank_request_list.txt") as fr:
    empty = fr.read(1)
    if not empty:
        f.write(tr)
        f.close
    else:
        tr = "#" + tr
        f.write(tr)
        f.close

我现在需要弄清楚如何删除索引0处的项目

我还需要实现这段代码:

# List Length
list_length = len(treq)
print "There are %d tanks in the queue." % (list_length)

# Next 5 Requests
print "Next 5 Requests are:"
def tank_lst(x):
  for i in range(5):

    print "- " + x[i]

# Run Tank_request    
tank_lst(treq)

以下将返回正确答案但不写。

def del_played(tank):
    del tank[0]
    return tank

tanks = treq
print del_played(tanks)
python python-2.7
1个回答
0
投票

首先,删除内容使用truncate函数从文件中删除内容,然后将新列表写入其中。

© www.soinside.com 2019 - 2024. All rights reserved.