python 声明变量时使用if else

r6hnlfcb  于 2022-12-21  发布在  Python
关注(0)|答案(3)|浏览(145)

我正在写一个脚本发送代理文件从一个目录到电报通道。在一些好人的帮助下,我几乎能够完成它

from telegram import Bot, InputMediaDocument

BOT_TOKEN = "xxx"
CHAT_ID = xxx

def main():
    bot = Bot(BOT_TOKEN)
    file_paths = (
        "proxy/proxies/http.txt",
        "proxy/proxies/socks4.txt",
        "proxy/proxies/socks5.txt"
    )
    path_http = "proxy/proxies/http.txt"
    with open(path_http,'rb') as file_http:
     http_count = len(file_http.readlines())
    file_http.close()

    path_socks4 = 'proxy/proxies/socks4.txt'
    with open(path_socks4,'rb') as file_socks4:
     socks4_count = len(file_socks4.readlines())
    file_socks4.close()

    path_socks5 = 'proxy/proxies/socks5.txt'
    with open(path_socks5,'rb') as file_socks5:
     socks5_count = len(file_socks5.readlines())
    file_socks5.close()

    text = f"Total HTTPs:{http_count}\nTotal SOCKS4:{socks4_count}\nTotal SOCKS5:{socks5_count}"

  media_group = list()
  for f in file_paths:
        with open(f, "rb") as fin:
         caption = text if f == "proxy/proxies/http.txt" else ''
        fin.seek(0)
        media_group.append(InputMediaDocument(fin, caption=caption))

  bot.send_media_group(CHAT_ID, media=media_group)

if __name__ == "__main__":
    main()

发生的问题

media_group = list()
                    ^
IndentationError: unindent does not match any outer indentation level

我尝试的是将这3个代理文件与标题一起发送到第一个代理文件

xnifntxz

xnifntxz1#

您的程式码有几个问题:
1.缺乏适当的压痕
1.括号闭合不当
你在path_http = 'proxy/proxies/http.txt'行得到的SyntaxError是因为你没有把上一行(你初始化变量file_paths的那一行)的括号括起来。
我也预见到当你试图用open函数打开文件时会遇到问题,你可以在这里找到一些例子:https://www.programiz.com/python-programming/file-operation

dz6r00yl

dz6r00yl2#

`file_paths = (
        "proxy/proxies/http.txt",
        "proxy/proxies/socks4.txt",
        "proxy/proxies/socks5.txt"
`

无效的语法错误可能是因为)在结尾不存在。
尝试添加(““)caption = text if f == "proxy/proxies/http.txt" else '',因为它是字符串。

vuktfyat

vuktfyat3#

在一些好人的帮助下,我使它工作,如果有任何简单的版本免费分享

from telegram import Bot, InputMediaDocument

BOT_TOKEN = "xxxxx"
CHAT_ID = -1000000

def http():
    path_http = 'proxy/proxies/http.txt'
    with open(path_http, 'rb') as file_http:
        http_count = len(file_http.readlines())
    return http_count
def socks4():
    path_socks4 = 'proxy/proxies/socks4.txt'
    with open(path_socks4, 'rb') as file_socks4:
        socks4_count = len(file_socks4.readlines())
    return socks4_count
def socks5():
    path_socks5 = 'proxy/proxies/socks5.txt'
    with open(path_socks5, 'rb') as file_socks5:
        socks5_count = len(file_socks5.readlines())
    return socks5_count        
http_count = http()
socks4_count = socks4()
socks5_count = socks5()
text = f"Total HTTPs:{http_count}\nTotal SOCKS4:{socks4_count}\nTotal SOCKS5: {socks5_count}"

def main():
    bot = Bot(BOT_TOKEN)
    file_paths = (
        "proxy/proxies/http.txt",
        "proxy/proxies/socks4.txt",
        "proxy/proxies/socks5.txt"
    )
    media_group = list()
    for f in file_paths:
        with open(f, "rb") as fin:
           caption = text if f == "proxy/proxies/socks5.txt" else '' 
           fin.seek(0)
           media_group.append(InputMediaDocument(fin, caption=caption))
    bot.send_media_group(CHAT_ID, media=media_group)

if __name__ == "__main__":
    main()

相关问题