类型错误: 打开文件时'str'对象不能解释为整数



我有下一个代码

import requests, os, sys
from colorama import *
foo = f"{Fore.GREEN}[+] {Fore.WHITE}"
def subhyp():
try:
kukipbcxz = open('lala.txt','a')
rp = requests.get("example.com")
jui = str(rp.text)
print(jui, file = a)
print(f"n{foo}Results saved in lala.txt")
a.close()
except KeyboardInterrupt:
pass
print("""
1.- someoption1
2.- someoption2
3.- sometestoption3
""")
a = str(input("Option = "))
if a == "1":
print("someoption1")
elif a == "2":
print("someoption2")
elif a == "3":
subhyp()

当我执行代码并选择sometestoption3时,我在终端中得到以下错误

a = open("lala.txt",'w')
TypeError: 'str' object cannot be interpreted as an integer

我试着用下一个代码将(a(转换为str,我在终端上得到了下一个输出a=打开(str("lala.txt","w"(输出:

a = open(str("lala.txt",'w'))
TypeError: decoding str is not supported

我不知道为什么会发生这种情况,如果有人能为我提供一个解决方案,我将不胜感激,感谢阅读:(

我的水晶球说from colorama import *(或者可能是您没有向我们展示的另一个导入(意外地用os.open覆盖了内置的open,这确实会导致您引用的错误。

>>> from os import open
>>> open("foo.txt", "a")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object cannot be interpreted as an integer
>>>

此外,您将使用(无意义的(变量名kukipbcxz打开文件,但您将使用file=a,因此这也不起作用。(IDE应该突出显示未使用的kukipbcxz和未分配的a。如果没有,请切换IDE…(

最好将with与文件一起使用,所以总而言之,请尝试:

import requests
from colorama import Fore
foo = f"{Fore.GREEN}[+] {Fore.WHITE}"

def subhyp():
try:
rp = requests.get("example.com")
except KeyboardInterrupt:
return
with open('lala.txt', 'a') as a:
jui = str(rp.text)
print(jui, file=a)
print(f"n{foo}Results saved in lala.txt")

print("""
1.- someoption1
2.- someoption2
3.- sometestoption3
""")
a = str(input("Option = "))
if a == "1":
print("someoption1")
elif a == "2":
print("someoption2")
elif a == "3":
subhyp()

最新更新