TypeError:必须是str,而不是NoneType。但是把变量改成字符串

2024-04-27 04:58:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我在做一个程序,它可以改变你在python中的MAC地址。我真的在努力克服这个错误。这是我目前为止的代码:

import subprocess
import optparse

parser = optparse.OptionParser()

parser.add_option("-i", "--interface", dest="interface", help="Interface to change its MAC adress")
parser.add_option("-m", "--mac", dest="new_mac", help="New MAC adress")

(options, arguments) = parser.parse_args()

interface = options.interface
new_mac = options.new_mac

print("[+] Changing MAC adress for " + interface + " to " + new_mac)

subprocess.call(["ifconfig", interface, "down"])
subprocess.call(["ifconfig", interface, "hw", "ether", new_mac])
subprocess.call(["ifconfig", interface, "up"])

这是我得到的错误:

^{pr2}$

这个错误是指我的打印线。我已经试过了:

print("[+] Changing MAC address for " + str(interface) + " to " + str(new_mac))

,但是没有用。 当我这么做的时候,我得到了一个错误:

TypeError: expected str, bytes or os.Pathlike object, Not NoneType

在整个代码中将optparse更改为argparse,并没有解决问题。 我没有给出任何命令行参数,因为我希望用户能够自己给出它们。在

希望你能帮助我。谢谢。在


Tags: to代码parsernewmac错误callinterface