Python: change CWD of script bundled with py2app
Question: My script works fine when ran with `python`, but after I build it with
`py2app`, files and folders are created in `.app/Contents/Resources`. I use
`os.getcwd()` to find out where the script is located.

How can I fix this and make sure my files are created in the app containing
directory?

**Hacky fix**

Here's a hack that works:

    
    
    if re.search('([^/]+$)', cwd).group(0) == "Resources":
        mkFldr(cwd[0:-27] + fldr)         #this is a try except function
        sanExt(extPath, str(cwd[0:-27] + fldr + "/" + extName + "_san.csv"))
    else:
        mkfldr(fldr)
        sanExt(extPath, str(cwd + "/" + fldr + "/" + extName + "_san.csv"))
    


Answer: If I understand what you're asking correctly I use something similar to the
following to handle this. The 'frozen' part changes the behaviour depending on
whether you're running the script or a py2exe/py2app executable.

    
    
    import sys
    import os.path
    
    frozen = getattr(sys, 'frozen', '')
    
    if frozen:
        MyFolder = os.path.dirname(sys.executable)
    else:
        Myfolder = os.getcwd()
    

