#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    6
    Rep Power
    0

    Exiting a program with a return value


    HI ,

    I am a new bie in python ..can you tell me how should i exit a program with the value returned by the main..

    currently my program is like :


    def main()

    --
    --
    return value

    if __name__ == "__main__":
    main()
    sys.exit(main())

    this returns thevaluethough but errors out .. any ideas of how to fix this?
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,890
    Rep Power
    481
    Don't know what you mean by "errors out"---probably bad code in the part you didn't show. Also you likely don't want to call main twice.

    Code:
    import sys
    
    def main(*args, **kwargs):
        return 1
    
    sys.exit(main())
    Wow, aren't the code tags neat!
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    6
    Rep Power
    0
    lets say that i want to use the value returned by my python program somewhere so the error is something like this

    Error
    Traceback (most recent call last):
    File "C:/Python27/gra", line 66, in <module>
    main()
    File "C:/Python27/gra", line 61, in main
    sys.exit(message)
    SystemExit: message1


    where as the code is like this


    def main()

    message = 'message1'

    return message


    if __name__ == "__main__":
    main()
    sys.exit(main())


    so basically i need to do 2 things :
    1. program should exit with no errors
    2. it should return the value of the message that is message1 ..

    let me know if this helps !
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,890
    Rep Power
    481
    The argument to sys.exit is an integer. You need an interaction between python and your operating system. If you're using bash I'll help you more. Otherwise, I prefer to not support windows. This is an obstinacy issue, not knowledge deficit.
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    6
    Rep Power
    0
    so lets say if we forget about exiting the program..the only thing we are concerned is who to get that return value after the program has finished execution ..
  10. #6
  11. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,890
    Rep Power
    481
    And your command shell is...?
    [code]Code tags[/code] are essential for python code and Makefiles!
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    6
    Rep Power
    0
    Originally Posted by b49P23TIvg
    And your command shell is...?
    its ssh i think ..
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    6
    Rep Power
    0
    My command shell is ssh
  16. #9
  17. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,890
    Rep Power
    481
    OK, if you want to capture information written to stdout (the normal destination of print) then

    $ RESULT=$( python3 myProgram.py )

    stores the stdout stream into the shell variable RESULT.

    $ echo $RESULT # displays it on console


    If you want the status code preserved,

    Code:
    $ python3 myProgram.py
    $ STATUS=$? # STATUS is the integer argument to python's sys.exit
    [code]Code tags[/code] are essential for python code and Makefiles!
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    6
    Rep Power
    0
    actually no ....

    this is the actual thing i am confused with:
    this is how my python script is :
    def get()
    return value


    def main()
    var = get()


    if __name__ == "__main__":
    main()


    so i want this program to output the return value as i need to use this value somewhere else... and this value being returned is a string ...?
  20. #11
  21. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,890
    Rep Power
    481
    $ RESULT=$( python3 myProgram.py )


    Now you need to put information onto the stdout stream.


    print(var)


    it'll be a "string".
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo