问:如何从事件中增加文件创建



我试图使用telethon事件为Telegram上的每一条新消息创建一个txt文件。我想要像OIF,OIF1,OIF2,OIF3这样的txt文件。。。对于我收到的每一条信息

感谢您的帮助
这是我的代码

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:UsersUSERPycharmProjectsrakna2OIF%s.txt' %i, "w")
f.write("buy b a")
f.close()
if Direction == 'sell':
f= open(r'C:UsersUSERPycharmProjectsrakna2OIF%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()

最新更新