pythonindentation

"NameError: name 'main' is not defined."


import sys

def Cat(filename):

    f = open(filename,'rU')
    text = f.read()
    print ("text")
    f.close()

    def main():
        Cat(sys.argv[1])  

if __name__ == '__main__':
    main()

I keep getting the following error shown below:

NameError: name 'main' is not defined.

Solution

  • main is not indented properly. It should have no indentation. Now it is only accessible from Cat.

    import sys
    
    def Cat(filename):
    
        f = open(filename,'rU')
        text = f.read()
        print ("text")
        f.close()
    
    def main():
        Cat(sys.argv[1])  
    
    if __name__ == '__main__':
        main()