【问题标题】:TypeError: must be str, not NoneType. But changing my variable to a string didn't workTypeError:必须是 str,而不是 NoneType。但是将我的变量更改为字符串不起作用
【发布时间】:2019-01-11 09:48:50
【问题描述】:

我正在开发一个程序,它会在 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"])

这是我得到的错误:

TypeError: must be str, not NoneType

此错误与我的打印行有关。我已经尝试过:

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

但这没有用。 当我这样做时,我收到了这个错误:

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

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

希望你能帮助我。谢谢。

【问题讨论】:

  • 请把完整的代码和问题本身的错误贴出来
  • 请在问题中包含您的代码和错误,而不是图像,而不是链接。
  • 粘贴您的代码。突出显示它。按 Ctrl+K。
  • 你的程序需要命令行参数(接口和mac地址)。你没有通过。
  • 另外,optparse 已经过时了。你应该使用argparse

标签: python typeerror


【解决方案1】:

事实证明我的代码非常好。在运行代码之前,我只需要先在终端中传递一些命令行。相反,我想运行代码然后传递参数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多