Return variables from modules ??

Skip Montanaro skip at pobox.com
Tue Oct 21 17:17:54 EDT 2003


    Rigga> I am new to Python and am currentky just playing with some simple
    Rigga> functions however I can not work out how to return a variable
    Rigga> back from a module...

This is not possible.  Modules are not callable objects.  They define
namespaces which hold other objects, among which are functions, which are
callable.

Your module becomes something like

    import sys import os

    def chkpth(FilePath):

            if os.path.exists(FilePath):
                    # File location exists
                    AccFlag = os.access(FilePath,os.R_OK | os.X_OK | os.W_OK)

                    if (AccFlag):
                            #  Cool you have FULL access to the location
                            chkpth = "OK"
                            reply = 'stop'
                    else:
                            # You do not have access
                            chkpth = "DENIED"
                            reply = 'repeat'

            else:
                    # No files found exiting...
                    chkpth = "NOT FOUND"
                    reply = 'repeat'

            return chkpth

    if __name__ == "__main__":
        # Check that the folder is accessible and writeable
        reply = 'repeat'
        while reply == 'repeat' :
                FilePath = raw_input("Enter path to files: ")

                print chkpth(FilePath)     # used to show me chkpth result
                print reply     # always prints repeat no matter what!

Suppose the above code is in chkpth.py.  You can execute

    python chkpth.py

to run it standalone, or from other Python code write:

    import chkpth

    ...

    result = chkpth.chkpth(somepath)

Skip





More information about the Python-list mailing list