如何解决此错误? 代码是
from datetime import datetime
with open("1.txt") as f:
lines = f.readlines()
lines[1] = datetime.today().strftime('%A %d %B %Y at %I:%M %p')
with open("1.txt", "w") as f:
f.writelines(lines)
错误
File "test.py", in <module>
lines[1] = datetime.today().strftime('%A %d %B %Y')
IndexError: list assignment index out of range```
答案 0 :(得分:0)
1.txt的内容是什么?假设文件中只有一行,并且您要更新该行,则必须使用第一个索引为[0]的行,因此:
async with session.get(image_url) as resp:
如果要添加行,则必须使用
lines[0] = datetime.today().strftime('%A %d %B %Y at %I:%M %p')