Forum Archive

How To Resolve A File Path?

MartinPacker

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:

1) metadata.md
2) ./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.

ccc

os.path.join(os.path.abspath(os.curdir), 'metadata.md')

MartinPacker

Thanks @ccc. And that resolves eg ../ to "parent directory" ?

MartinPacker

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.

ccc

https://docs.python.org/2/library/os.html#os.curdir

os.curdir == '.'
os.pardir == '..'