python2报错NameError: name 'characters1' is not defined ``` # encoding: utf-8 import time ### 时间 import os import random import subprocess class root_password: Ap='QAZWSXEDCRFVTGBYHNUJMIKOLP' Bp='qazwsxedcrfvtgbyhnujmikolp' Cp='!@#$%^&*' Dp='0123456789' a=time.strftime('%Y-%m-%d %H:%M',time.localtime(time.time())) characters = 'QAZWSXEDCRFVTGBYHNUJMIKLOP' Dpass = random.choice(characters) print(Dpass) characters = '!@$%^&*' #random_characters = random.choice(characters) #fpass = ''.join(random_characters) fpass = ''.join(random.choice(characters)) print(fpass) characters = Bp + Ap #passfa1 = ''.join(random.choices(characters, k=2)) passfa1 = ''.join(random.choice(characters) for _ in range(2)) print(passfa1) characters = Bp + Dp #passfa2 = ''.join(random.choices(characters, k=2)) passfa2 = ''.join(random.choice(characters) for _ in range(2)) print(passfa2) characters = Bp + Cp #passfa3 = ''.join(random.choices(characters, k=3)) passfa3 = ''.join(random.choice(characters) for _ in range(3)) print(passfa3) characters = Ap + Dp #passfa4 = ''.join(random.choices(characters, k=2)) passfa4 = ''.join(random.choice(characters) for _ in range(2)) print(passfa4) characters = Ap + Cp #passfa5 = ''.join(random.choices(characters, k=2)) passfa5 = ''.join(random.choice(characters) for _ in range(2)) print(passfa5) characters = Dp + Cp #passfa6 = ''.join(random.choices(characters, k=3)) passfa6 = ''.join(random.choice(characters) for _ in range(3)) print(passfa6) NEWPASS=Dpass+passfa3+passfa6+fpass+passfa1+passfa4+passfa5 print(NEWPASS) #command = f'echo {NEWPASS} | sudo passwd --stdin root'#python3.6 command = 'echo '+NEWPASS+' | sudo passwd --stdin root' subprocess.run(command, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) #command1 = f'echo {a}--root >> /root/pass.log'#python3.6 #command2 = f'echo {NEWPASS} >> /root/pass.log'#python3.6 command1 = 'echo '+a+'--root >> /root/pass.log' command2 = 'echo '+NEWPASS+' >> /root/pass.log' subprocess.run(command1, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) subprocess.run(command2, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) ```
时间: 2024-04-13 11:28:09 浏览: 224
在你的代码中,出现了类似的错误。在定义 `characters1` 变量之前,你需要在代码的开头添加以下行:
```
characters1 = ''
```
这样就会定义一个空字符串 `characters1`,然后你可以在后面的代码中对它进行操作。确保将这行代码放在 `characters1` 第一次出现之前。
此外,还需要注意,你使用了 `print()` 函数来打印变量的值。在类定义中直接使用 `print()` 是不推荐的做法。如果你想在类外部打印这些变量的值,可以在类定义后添加以下代码:
```
rp = root_password()
print(rp.Dpass)
print(rp.fpass)
print(rp.passfa1)
print(rp.passfa2)
print(rp.passfa3)
print(rp.passfa4)
print(rp.passfa5)
print(rp.passfa6)
print(rp.NEWPASS)
```
这样就可以在类外部打印这些变量的值了。记得将这段代码放在类定义之后。
相关问题
使用pyinstaller打包exe后运行出现报错NameError: name 'null' is not defined
这个报错通常是因为 Python 2.x 中的 `null` 关键字在 Python 3.x 中已经被移除,所以在 Python 3.x 中使用 `null` 会出现这种错误。
对于这个问题,你可以尝试以下几个解决方案:
1. 将所有的 `null` 替换为 `None`。
2. 导入 `null` 关键字,例如 `from types import NoneType as null`。
3. 使用 Pyinstaller 的 `--hidden-import` 参数,手动添加缺少的模块。例如,如果你使用了 Flask 库,你可以使用以下命令:`pyinstaller --hidden-import=flask your_script.py`。
希望这些解决方案对你有帮助。
python2报错NameError: name 'characters' is not defined ``` # encoding: utf-8 import time ### 时间 import os import random import subprocess class root_password: Ap='QAZWSXEDCRFVTGBYHNUJMIKOLP' Bp='qazwsxedcrfvtgbyhnujmikolp' Cp='!@#$%^&*' Dp='0123456789' a=time.strftime('%Y-%m-%d %H:%M',time.localtime(time.time())) characters = 'QAZWSXEDCRFVTGBYHNUJMIKLOP' Dpass = random.choice(characters) print(Dpass) characters = '!@$%^&*' #random_characters = random.choice(characters) #fpass = ''.join(random_characters) fpass = ''.join(random.choice(characters)) print(fpass) characters = '' characters = Bp + Ap #passfa1 = ''.join(random.choices(characters, k=2)) passfa1 = ''.join(random.choice(characters) for _ in range(2)) print(passfa1) characters = Bp + Dp #passfa2 = ''.join(random.choices(characters, k=2)) passfa2 = ''.join(random.choice(characters) for _ in range(2)) print(passfa2) characters = Bp + Cp #passfa3 = ''.join(random.choices(characters, k=3)) passfa3 = ''.join(random.choice(characters) for _ in range(3)) print(passfa3) characters = Ap + Dp #passfa4 = ''.join(random.choices(characters, k=2)) passfa4 = ''.join(random.choice(characters) for _ in range(2)) print(passfa4) characters = Ap + Cp #passfa5 = ''.join(random.choices(characters, k=2)) passfa5 = ''.join(random.choice(characters) for _ in range(2)) print(passfa5) characters = Dp + Cp #passfa6 = ''.join(random.choices(characters, k=3)) passfa6 = ''.join(random.choice(characters) for _ in range(3)) print(passfa6) NEWPASS=Dpass+passfa3+passfa6+fpass+passfa1+passfa4+passfa5 print(NEWPASS) #command = f'echo {NEWPASS} | sudo passwd --stdin root'#python3.6 command = 'echo '+NEWPASS+' | sudo passwd --stdin root' subprocess.run(command, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) #command1 = f'echo {a}--root >> /root/pass.log'#python3.6 #command2 = f'echo {NEWPASS} >> /root/pass.log'#python3.6 command1 = 'echo '+a+'--root >> /root/pass.log' command2 = 'echo '+NEWPASS+' >> /root/pass.log' subprocess.run(command1, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) subprocess.run(command2, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) ```
你在代码中定义了一个变量 `characters`,但在使用它之前没有将其赋值。这导致了 `NameError: name 'characters' is not defined` 错误。
为了解决这个问题,你需要在使用 `characters` 之前先将其赋值。你可以将 `characters` 的赋值放在定义类之前,以确保它在整个类内都可用。例如,你可以将以下行添加到你的代码的开头:
```python
characters = ''
```
这样就会将 `characters` 初始化为空字符串,然后你可以在后面的代码中对其进行操作。确保将这行代码放在其他变量的定义之前。
阅读全文