Re: Return variables from modules ??

From: Dave Kuhlman (dkuhlman_at_rexx.com)
Date: 10/21/03


Date: Tue, 21 Oct 2003 14:38:05 -0700

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@rexx.com


Relevant Pages

  • Re: Hot to keep file copy from stopping?
    ... - drill into the failed folder and repeat within there ... I've have folks tell me with a straight face that "copying ... special tools; ...
    (microsoft.public.windowsxp.security_admin)
  • Re: Cannot create "sys" folder anywhere
    ... I've just encountered this problem as well, however I'm running Windows ... I cannot create a "sys" folder on and local drive or ... folder, and now I can't read it's contents... ... unprivileged user provided I have permission to do so. ...
    (microsoft.public.windows.server.general)
  • Re: Cannot create "sys" folder anywhere
    ... I cannot create a "sys" folder on and local drive or ... folder, and now I can't read it's contents... ... can create and delete folders called sys on my Server 2003 box as an ... unprivileged user provided I have permission to do so. ...
    (microsoft.public.windows.server.general)
  • Re: Scripting "Empty Cache" in Entourage
    ... to Empty Cache. ... This should do what you want assuming you only have one Exchange account ... this script will recursively work its way down the folder hierarchy, ... repeat with aFolder in every folder of anAccount ...
    (microsoft.public.mac.office.entourage)
  • Re: Entourage and AppleScript in Snow Leopard. Compatibility.
    ... We suppose that the problem is caused by AppleScript when running under Snow Leopard. ... tell application "Microsoft Entourage" ... repeat with j from 1 to numFolders ... set pFolderMessages to count messages in folder id folderID ...
    (microsoft.public.mac.office.entourage)