Python SyntaxError:无效语法elif语句

时间:2021-10-06 22:44:05

So I am pretty new to python, I am familiar with Java, C and Ruby.

所以我对python很陌生,我熟悉Java, C和Ruby。

I tried compiling a script for Kali to fix the RFkill issue for wifi devices since Kali does not have an RFKill.

我尝试为Kali编写一个脚本来修复wifi设备的RFkill问题,因为Kali没有RFkill。

#!/usr/bin/python
# replacement for rfkill util, which is missing in kali
# By: Geist

from sys import argv

if(argv[1] == "unblock"):
    x = open("/sys/class/rfkill/rfkill%s/soft" % argv[2], "w")
x.seek(0)
x.write('0')

elif(argv[1] == "block"):
    x = open("/sys/class/rfkill/rfkill%s/soft" % argv[2], "w")
x.seek(0)
x.write('1')


print("interface %s %sed" % (argv[2], argv[1]))

I did not write this but I am trying to run it and I keep getting a SyntaxError: invalid syntax under elif(argv[1] == "block"):

我没有写这个,但是我正在尝试运行它,我不断地得到一个SyntaxError: elif下的无效语法(argv[1] = "block"):

I am assuming this has something to do with improper indentation, if anyone could be as kind to let me know what I am doing wrong and why that would be great!

我假设这与不适当的缩进有关,如果有人能让我知道我做错了什么,为什么那将是伟大的!

3 个解决方案

#1


2  

Indentation matters in Python. You have unindented lines between your if block and your elif block. These will cause a SyntaxError because you've effectively got an elif block without an if block.

在Python中缩进问题。在if块和elif块之间有未缩进的行。这将导致SyntaxError,因为您已经有效地获得了一个没有if块的elif块。

Either indent your lines so they match the if block, or use a second if statement rather than elif. Looking at your code, I'd imagine you'll want to indent them otherwise you would get NameErrors. In this case it becomes:

要么缩进您的行,以便它们与if块匹配,要么使用第二个if语句,而不是elif。看看你的代码,我想你会想缩进它们,否则你会得到NameErrors。在这种情况下,它变成:

#!/usr/bin/python
# replacement for rfkill util, which is missing in kali
# By: Geist

from sys import argv

if(argv[1] == "unblock"):
    x = open("/sys/class/rfkill/rfkill%s/soft" % argv[2], "w")
    x.seek(0)
    x.write('0')

elif(argv[1] == "block"):
    x = open("/sys/class/rfkill/rfkill%s/soft" % argv[2], "w")
    x.seek(0)
    x.write('1')


print("interface %s %sed" % (argv[2], argv[1]))

#2


0  

Indentation issue in your code, change according to your algorithm-

缩进问题在你的代码,改变根据你的算法-

For e.g.

如。

#!/usr/bin/python
# replacement for rfkill util, which is missing in kali
# By: Geist

from sys import argv

if(argv[1] == "unblock"):
    x = open("/sys/class/rfkill/rfkill%s/soft" % argv[2], "w")
    x.seek(0)
    x.write('0')
elif(argv[1] == "block"):
    x = open("/sys/class/rfkill/rfkill%s/soft" % argv[2], "w")
    x.seek(0)
    x.write('1')


print("interface %s %sed" % (argv[2], argv[1]))

#3


0  

Here's a slightly cleaner version of that script.

这是该脚本的一个稍微干净一点的版本。

#! /usr/bin/env python

from sys import argv

def main():
    try:
        cmd = ("unblock", "block").index(argv[1])
    except ValueError:
        print("Bad command: %s" % argv[1])
        exit(1)

    fname = "/sys/class/rfkill/rfkill%s/soft" % argv[2]
    with open(fname, "w") as f:
        f.seek(0)
        f.write(str(cmd))

    print("Interface %s %sed" % (argv[2], argv[1]))


if __name__ == "__main__":
    main()

#1


2  

Indentation matters in Python. You have unindented lines between your if block and your elif block. These will cause a SyntaxError because you've effectively got an elif block without an if block.

在Python中缩进问题。在if块和elif块之间有未缩进的行。这将导致SyntaxError,因为您已经有效地获得了一个没有if块的elif块。

Either indent your lines so they match the if block, or use a second if statement rather than elif. Looking at your code, I'd imagine you'll want to indent them otherwise you would get NameErrors. In this case it becomes:

要么缩进您的行,以便它们与if块匹配,要么使用第二个if语句,而不是elif。看看你的代码,我想你会想缩进它们,否则你会得到NameErrors。在这种情况下,它变成:

#!/usr/bin/python
# replacement for rfkill util, which is missing in kali
# By: Geist

from sys import argv

if(argv[1] == "unblock"):
    x = open("/sys/class/rfkill/rfkill%s/soft" % argv[2], "w")
    x.seek(0)
    x.write('0')

elif(argv[1] == "block"):
    x = open("/sys/class/rfkill/rfkill%s/soft" % argv[2], "w")
    x.seek(0)
    x.write('1')


print("interface %s %sed" % (argv[2], argv[1]))

#2


0  

Indentation issue in your code, change according to your algorithm-

缩进问题在你的代码,改变根据你的算法-

For e.g.

如。

#!/usr/bin/python
# replacement for rfkill util, which is missing in kali
# By: Geist

from sys import argv

if(argv[1] == "unblock"):
    x = open("/sys/class/rfkill/rfkill%s/soft" % argv[2], "w")
    x.seek(0)
    x.write('0')
elif(argv[1] == "block"):
    x = open("/sys/class/rfkill/rfkill%s/soft" % argv[2], "w")
    x.seek(0)
    x.write('1')


print("interface %s %sed" % (argv[2], argv[1]))

#3


0  

Here's a slightly cleaner version of that script.

这是该脚本的一个稍微干净一点的版本。

#! /usr/bin/env python

from sys import argv

def main():
    try:
        cmd = ("unblock", "block").index(argv[1])
    except ValueError:
        print("Bad command: %s" % argv[1])
        exit(1)

    fname = "/sys/class/rfkill/rfkill%s/soft" % argv[2]
    with open(fname, "w") as f:
        f.seek(0)
        f.write(str(cmd))

    print("Interface %s %sed" % (argv[2], argv[1]))


if __name__ == "__main__":
    main()