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

发布于 2025-01-13 01:44:58 字数 751 浏览 3 评论 0原文

我尝试使用 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()

I tried to create a txt file for every new message from Telegram using telethon event.
I would like to have txt file like OIF ,OIF1, OIF2,OIF3 ... for every message I get

Thanks for help
That's my code

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()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我爱人 2025-01-20 01:44:58
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()
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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文