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.
How To Resolve A File Path?
-
In a Python script I'd like to be able to take a path and resolve it so I can use editor.get_file_contents() for it.
Two examples:
- metadata.md
- ./metadata.md
I can use get_selected_folder() for the first and this works well - concatenating /metadata.md to the path returned and using the root.
How do I do more complicated cases such as the second? Do I individually look for ./ and ../ and manipulate the path from there?
Any ideas? This must be a pretty standard thing to do.
-
os.path.join(os.path.abspath(os.curdir), 'metadata.md')
-
Thanks @ccc. And that resolves eg ../ to "parent directory" ?
-
And I suspect os.curdir isn't the directory I want. In my use case I want it to be relative to the document in the frame. Hence my use of get_selected_folder.
With that adjustment I tested successfully with ./metadata.md.
So thanks again @ccc.
-
https://docs.python.org/2/library/os.html#os.curdir
os.curdir == '.'
os.pardir == '..'