首页 > 解决方案 > 我的代码不会写入未受保护的 txt 文件

问题描述

我有一段用于实时子计数的代码,它运行良好,但我想将这些结果添加到一个名为“history.txt”的文件中,但是当我运行代码时,即使我离开它很长一段时间,为了循环循环很多次,我的 txt 文件保持为空。

(代码如下)(我知道它的草率不要评判我)

import requests
import time
import os
from time import gmtime, strftime

open("history.txt", "w").close()

histTx = open("history.txt", "a")

def simp():
  global histTx
  u = input("Input the URL of the channel page:\n")
  p = True
  while p:
    try:
      u.index("http")
    except ValueError:
      u = "http://" + u
    response = requests.get(u)
    r = str(response.content).lower()

    locator = r.index("subscribers")
    goBack = locator
    while not r[goBack] == "\"":
      goBack -= 1
    goBack += 1
    os.system('clear')
    print("\033[1;36;48m", r[goBack : locator].upper())
    print("---------\n")
    time.sleep(2.5)
    os.system('clear')
    histTx.write(str(r[goBack : locator].upper()))

def adv():
  global histTx
  u = input("Input the URL of the channel page (input -1 for simple mode):\n")
  if u == "-1":
    simp()
  else:
    p = True
    while p:
      try:
        u.index("http")
      except ValueError:
        u = "http://" + u
      response = requests.get(u)
      r = str(response.content).lower()

      locator = r.index("subscribers")

      goBack = locator
      while not r[goBack] == "\"":
        goBack -= 1
      goBack += 1
      os.system('clear')
      print("That channel has \033[1;36;48m" + r[goBack : locator].upper() + "\033[0mSubscribers.\n")
      print("---------\n")
      time.sleep(2.5)
      os.system('clear')
      histTx.write(str(r[goBack : locator].upper()))

adv()

(在你问之前,没有错误,history.txt确实存在。)

标签: python

解决方案


问题是它应该是def simp(histTx)and ,并且在运行它时def adv(histTx)应该有simp(histTx)and 。adv(histTx)


推荐阅读