Stop execution of a script called with execfile

后端 未结 3 1695
既然无缘
既然无缘 2021-02-07 06:37

Is it possible to break the execution of a Python script called with the execfile function without using an if/else statement? I\'ve tried exit(), but it doesn\'t a

3条回答
  •  梦毁少年i
    2021-02-07 07:39

    # script.py
    def main():
        print "Script starting"
        a = False
    
        if a == False:
            # Sanity checks. Script should break here
            #     
            return;
            # I'd prefer not to put an "else" here and have to indent the rest of the code
        print "this should not print"
        # lots of lines bellow
    
    if __name__ ==  "__main__":
        main();
    

    I find this aspect of Python (the __name__ == "__main__", etc.) irritating.

提交回复
热议问题