Q:how 从事件中增加文件创建?

Q:how to increment file creation from an event?

我尝试使用 telethon 事件为来自 Telegram 的每条新消息创建一个 txt 文件。 我想要 txt 文件,如 OIF、OIF1、OIF2、OIF3 ...对于我收到的每条消息

感谢帮助
那是我的代码

client.start()
@client.on(events.NewMessage(chats='tradingnava'))
async def my_event_handler(event):
    texte = (event.text)
    texte = texte.split(" ")
    Marche = texte[1]
    Direction = texte[2]
    i = 0
    while os.path.exists('OIF%s.txt' % i):
        i += 1
        if Direction == 'buy':
            f= open(r'C:\Users\USER\PycharmProjects\rakna2\OIF%s.txt' %i, "w")
            f.write("buy b a")
            f.close()
        if Direction == 'sell':
            f= open(r'C:\Users\USER\PycharmProjects\rakna2\OIF%s.txt" %i, "w")
            f.write("sell b a")
            f.close()
import os
import re

path = r'C:\Users\USER\PycharmProjects\rakna2\'
client.start()
@client.on(events.NewMessage(chats='tradingnava'))
async def my_event_handler(event):
    texte = (event.text)
    texte = texte.split(" ")
    Marche = texte[1]
    Direction = texte[2]

    allfile = [int(re.findall(f'\d+', fname)[0]) for fname in os.listdir(path) 
               if fname.startswith("OIF") and re.findall(f'\d+', fname)]
    try:
        s = str(max(allfile) + 1)
    except ValueError:
        allfile = [0]
    
    with open(r'OIF%s.txt' %str(max(allfile) + 1), 'w') as f:
        Direction = 'buy'
        if Direction == 'buy':
            f.write("buy b a")
            f.close()
        elif Direction == 'sell':
            f.write("sell b a")
            f.close()
        else:
            f.write(f"Direction Error {Direction}")
            f.close()