python - 我的代码不会写入未受保护的 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确实存在。)
解决方案
问题是它应该是def simp(histTx)
and ,并且在运行它时def adv(histTx)
应该有simp(histTx)
and 。adv(histTx)
推荐阅读
- python-3.x - 确定系统用户是否在Windows本地存在的正确方法是什么?
- java - 为什么我必须禁用 csrf 令牌才能使用邮递员发布数据?
- asp.net-core - 如何在模型验证中验证非法字段
- android - 如何使用 OSMDroid 检查当前位置和 KML 点之间的距离?
- git - Git推送到一个拥有新分支的分支
- postgresql - 如何从 postgresql 查询中的字节中获取一个字节字符?
- reactjs - 在 Material-UI v4 中为 `Box` 响应设置 m 和 p 的漂亮方法
- android - Android - 未授予 USE_BIOMETRIC
- javascript - Angular中的域名验证器?
- drools - 使用 wildfly16 升级到 jdk7 到 jdk11 时,流口水不起作用