omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    Get Documents path from appex

    Pythonista
    2
    5
    2440
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Webmaster4o
      Webmaster4o last edited by

      The

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

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

      1 Reply Last reply Reply Quote 0
      • omz
        omz last edited by

        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]
        
        1 Reply Last reply Reply Quote 1
        • Webmaster4o
          Webmaster4o last edited by

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

          1 Reply Last reply Reply Quote 0
          • omz
            omz last edited by

            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])
            
            1 Reply Last reply Reply Quote 1
            • Webmaster4o
              Webmaster4o last edited by 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 :)

              1 Reply Last reply Reply Quote 0
              • First post
                Last post
              Powered by NodeBB Forums | Contributors