A built in function similar to Python's os.path.join, that will specify a directory compatible with Windows/Linux?

This prevents one from having to manually change back slash to forward slash, if one decides to work on a project between Linux or Windows based directory systems.

Info here: os.path — Common pathname manipulations — Python 3.11.1 documentation
edit by karolus: use the higher level pathlib instead

E.g. the function

dirname, filename = os.path.split(os.path.abspath(sys.argv[0]))
# i.e.: dirname = /folder 1 or C:\folder 1
prj_location= os.path.join(dirname, 'Projects')

prj_location returns: /folder 1/Projects or C:\folder 1\Projects, if on windows

Hallo
nobody forces you not to use python!
But called from »inside« libreoffice sys.argv is empty!

from pathlib import Path
#points to $user-home/Projects
projectpath = Path.home() / "Projects"

And why not use a URL, as LibreOffice handles them well?

1 Like

IIRC Windows supports the forward slash instead of backslash already since the late 1990s (but I haven’t touched WIndows for a decade now). Except in that odd \\server\... notation.

In Basic I use something like this:

import uno
def getParsedURLStructFromSysPathName(sPathName):
    '''Returns a tuple with a boolean indicating success and the parsed url struct'''
    surl = uno.systemPathToFileUrl(sPathName)
    srv = uno.getComponentContext().ServiceManager.createInstance("com.sun.star.util.URLTransformer")
    url = uno.createUnoStruct('com.sun.star.util.URL')
    url.Complete = surl
    return srv.parseStrict(url)

The second element of the return tuple is a parsed LibreOffice: URL Struct Reference
with a protocol file:// and a Path and a Name
The concatenation url.Protocol + url.Path + sMyName gives a valid file URL with another file name.