Return variables from modules ??

Dave Kuhlman dkuhlman at rexx.com
Tue Oct 21 17:38:05 EDT 2003


Rigga wrote:

> Hi,
> 
> I am new to Python and am currentky just playing with some simple
> functions however I can not work out how to return a variable back
> from a module, probably easier if you see the code.. what I want
> it to do is to repeat the while loop until it is no longer equal
> to 'repeat', code is below, go easy on me!:
>
===========================================================================
> import sys import os
> # Check that the folder is accessible and writeable
> reply = 'repeat'
> while reply == 'repeat' :
>         FilePath = raw_input("Enter path to files: ")
> 
>         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
> 
>         print chkpth(FilePath)     # used to show me chkpth result
>         print reply     # always prints repeat no matter what!
> sys.exit()
> 
>
==============================================================================
> 
> I have tried setting reply as a global variable but to no avail, I
> assume that I need to pass the variable back from the chkpth
> module but I do not know how to, any help appreciated.
> 

A common way to do this is to put the code in your module into a
function (in that module) and at the end of that function, return
a value.  Then do:

    import mymodule
    reply = mymodule.myfunction()

And, here is how you might modify your module:

===========================================================================
import sys
import os

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

            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

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

Dave


-- 
Dave Kuhlman
http://www.rexx.com/~dkuhlman
dkuhlman at rexx.com




More information about the Python-list mailing list