#๐Ÿ”’ For Loop Not running

14 messages ยท Page 1 of 1 (latest)

stray birch
#

Hello, I am trying to code a bot that will read data and then delete it. I have attached my code. The first for loop is not running at all (I've put a lot of print statements in it). Can someone please tell me where I'm going wrong?

async def on_ready():
    print('Bot is now online and ready to roll')
    lines=[]
    outfile = open("file.txt", "w+")
    lines = outfile.readlines()
    final = ""
    print("I work")
    for line in outfile:
        print("I also work")
        date_str = line.strip()
        print(date_str)
        date_str = line[:18]
        print(date_str)
        date_format = '%Y-%m-%d %H:%M:%S'
        date_obj = datetime.datetime.strptime(date_str, date_format)
        print(date_obj)
        now = datetime.datetime.now()
        print(now)
        diff = date_obj-now
        print(diff)
        seconds = diff.total_seconds()
        print(seconds)
        if seconds > 0:
            print("running")
            await asyncio.sleep(seconds)
            await bot.wait_until_ready()
            remind_channel = bot.get_channel(channel)
            await remind_channel.send("<@role> time to post ad!")
            print("ran")
    for number, vert in enumerate(lines):
        if number != 0:
            final = final + vert
    outfile.write(final)
    outfile.close()
torpid skyBOT
#

@stray birch

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

ashen hound
#
    ========= ===============================================================
    Character Meaning
    --------- ---------------------------------------------------------------
    'r'       open for reading (default)
    'w'       open for writing, truncating the file first
    'x'       create a new file and open it for writing
    'a'       open for writing, appending to the end of the file if it exists
    'b'       binary mode
    't'       text mode (default)
    '+'       open a disk file for updating (reading and writing)
    ========= ===============================================================
stray birch
#

yes, I need it to be able to read and write

#

are you saying it should just be + and not w+?

ashen hound
#

truncating the file first
w empties the file, hence the for loop has nothing to loop on

stray birch
#

I tried doing r+, but then it said the content was unwritable

#

I just tried again, and the for loop still didn't work

ashen hound
#
    lines = outfile.readlines()
    final = ""
    print("I work")
    for line in outfile:
#

You have read the file, and moved the pointer to the end of the file.
Then you ask it to read from the pointer, which is at the end of the file.

#

Perhaps you should be looping through lines ๐Ÿคทโ€โ™‚๏ธ I haven't read through all of the code you gave.

stray birch
#

Oh, I see. should I do a seek then?

torpid skyBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.