Forum Archive

Get Documents path from appex

Webmaster4o

The

os.path.expanduser("~/Documents")

trick doesn't work from the app extension. Is there another way?

omz

If the script that you're running is in ~/Documents itself, you could get the path like this:

import os
docs_path = os.path.split(__file__)[0]
Webmaster4o

Right. Forgot about __file__. Since it's not in Documents, but in a subdirectory, I should be able to do a find.

omz

Here's something that should also work in subfolders:

import os

comps = __file__.split(os.sep)
doc_path = os.sep.join(comps[:comps.index('Documents')+1])
Webmaster4o

@omz thanks. I had just come up with this

def getPath():
    split=__file__.split('/')
    path=split[:split.index('Documents')+1]
    return '/'.join(path)

Yours is much cleaner :)

EDIT: oh wow, I just realized yours is almost exactly the same thing. I glanced at it and thought "Oh, it uses lots of os.path functions that I didn't know existed," but looking again it's basically exactly like my solution except using os.sep :)