if / elif问题-Python3

时间:2019-09-08 13:00:05

标签: python-3.x

好,来自这里: Read only 4 first letters of .txt file - Python3 这是我的第二个问题(对不起,这个问题。)。 我用4个字母成功读取了平台,现在我想要的是,如果platform = mt81或mt67继续并且如果没有退出的话。 为此,我使用了:

if platform == "mt81" or "mt67":
       continue
elif platform != "mt81" or != "mt67":
       wait = input("Incompatible CPU! BYE!")
       break

如果我执行python文件,没有任何错误,但是当我单击读取平台的选项时,什么也没有发生,并且返回菜单,但是如果我删除了if / elif工具正在运行但没有平台检测的情况。 >

我尝试过:

if "mt81" in platform or "mt67" in platform:
       continue
elif "mt81" not in platform or "mt67" not in platform:
       wait = input("Incompatible CPU! BYE!")
       break

但结果相同...

关于我可以做什么的任何想法? 再次感谢!

编辑 完整代码:

import os
import subprocess

# By R0rt1z2
# All the credits goes to diplomatic for create his excellent MTK-SU!

#set null variables
supported = ['mt81', 'mt67']

def menu():
        """
        Fucntion to clean
        """
        os.system('clear')
        print("\t Big thanks to diplomatic")
        print("\t1 - Root the Device")
        print("\t2 - Spawn Root Shell")
        print("\t3 - Exit")

while True:
        #show menu
        menu()

        #user sets option
        option = input("Select an option >> ")

        if option=="1":
             print("Detecting device information....")
             print("--------------------------------------")
             subprocess.call("adb shell getprop ro.boot.veritymode > verity.txt",shell=True)
             with open('verity.txt') as myfile:
               verity = myfile.read()
             subprocess.call("adb shell getprop debug.mtklog.netlog.Running > mtk.txt",shell=True)
             with open('mtk.txt') as myfile:
               mtk = myfile.read()
             subprocess.call("adb shell getprop ro.product.model > device.txt",shell=True)
             with open('device.txt') as myfile:
               device = myfile.read()
             subprocess.call("adb shell getprop ro.hardware > platform.txt",shell=True)
             with open('platform.txt', 'r') as myfile:
               platform = myfile.read(4)
             subprocess.call("adb shell getprop ro.product.cpu.abi > arch.txt",shell=True)
             with open('arch.txt') as myfile:
               arch = myfile.read()
             subprocess.call("adb shell getprop ro.build.version.release > android.txt",shell=True)
             with open('android.txt') as myfile:
               android = myfile.read()
             print("Device: " + device)
             print("ARCH: " + arch)
             print("Platform: " + platform)
             print("Android: " + android)
             print("--------------------------------------")
             if platform == "mt81" or "mt67":
               continue
             elif platform != "mt81" or != "mt67":
               wait = input("Incompatible CPU! BYE!")
               break
             if "enforcing" in verity:
                 print("Sorry! Your device seems to have DM-Verity, this method will not work. Exiting...")
                 break
             elif " " in verity:
                 continue
             # ty t0x1cSH
             if "arm64-v8a" in arch:
                print("--------------------------------------")
                print("Detected arm64 arch.. Pushing arm64 mtk-su & files")
                subprocess.call("adb push arm64/mtk-su arm64/root.sh arm64/su arm64/supolicy arm64/libsupol.so /data/local/tmp",shell=True)
                print("--------------------------------------")
                print("Pushed files succsefully!")
                print("Rooting the device...")
                subprocess.call("adb install files/SuperSU.apk",shell=True)
                subprocess.call("adb shell chmod 755 /data/local/tmp/mtk-su",shell=True)
                subprocess.call("adb shell chmod 755 /data/local/tmp/root.sh",shell=True)
                subprocess.call('adb shell /data/local/tmp/mtk-su -c "/data/local/tmp/root.sh"',shell=True)
                wait = input("PRESS ENTER TO CONTINUE.")
                os.system('clear')
             # ty t0x1cSH
             elif "armeabi-v7a" in arch:
                print("Detected armv7 arch.. Pushing armv7 mtk-su & files")
                subprocess.call("adb push arm/mtk-su arm/root.sh arm/su arm/supolicy arm/libsupol.so /data/local/tmp",shell=True)
                print("--------------------------------------")
                print("Pushed files succsefully!")
                print("Rooting the device...")
                subprocess.call("adb install files/SuperSU.apk",shell=True)
                subprocess.call("adb shell chmod 755 /data/local/tmp/mtk-su",shell=True)
                subprocess.call("adb shell chmod 755 /data/local/tmp/root.sh",shell=True)
                subprocess.call('adb shell /data/local/tmp/mtk-su -c "/data/local/tmp/root.sh"',shell=True)



        elif option=="2":
               print("ROOT SHELL SPAWNER")
               subprocess.call("adb shell getprop ro.product.cpu.abi > arch.txt",shell=True)
               with open('arch.txt') as myfile:
                  arch = myfile.read()
               print("Spawning Root Shell... \n For exit type 'exit' two times or Control+C for terminate the script")
               # ty again t0x1cSH :)
               if "arm64-v8a" in arch:
                   subprocess.call("adb push arm64/mtk-su /data/local/tmp",shell=True)
                   subprocess.call("adb shell chmod 755 /data/local/tmp/mtk-su",shell=True)
                   subprocess.call("adb shell /data/local/tmp/mtk-su",shell=True)
                   wait = input("PRESS ENTER TO CONTINUE.")
                   os.system('clear')
               elif "armeabi-v7a" in arch:
               # ty again again XD
                   subprocess.call("adb push arm/mtk-su /data/local/tmp",shell=True)
                   subprocess.call("adb shell chmod 755 /data/local/tmp/mtk-su",shell=True)
                   subprocess.call("adb shell /data/local/tmp/mtk-su",shell=True)
                   wait = input("PRESS ENTER TO CONTINUE.")
                   os.system('clear')

        elif option=="3":
               break

        else:
               print("")
               input("Incorrect option...\nPress any key to continue..")

2 个答案:

答案 0 :(得分:0)

platform的此值如何在文件中显示?

我认为platform是您必须比较其值的变量。因此,将platform用作字符串,可以检查platform是否以您拥有的两个字符串开头:'mt81''mt67'。 您可以做的是:

if platform.startswith('mt81') or  platform.startswith('mt67'):
    continue;
else:
    print("Incompatible platform.\n")

您正在做的是比较platform与'mt81'是否相同,考虑到其中可能有空格或特殊字符,这可能并非一直都是精确匹配。因此,最好使用字符串函数startswith

答案 1 :(得分:-1)

我想您不想继续传递代码,因此请尝试更改继续传递。 continue从头开始下一个循环,pass只是传递到下一个代码。

if platform in ('mt81', 'mt67'):
// if platform in set(supported):
    pass
else:
    wait = input("Incompatible CPU! BYE!")
    break

带注释的if句子用于根据需要使用定义的列表supported