Sublime Forum

Working with project folders


I try to create a configuration class for my plugin, that gathers information from the package, the workspace/project and a configuration file in the project directory. Now I can’t figure out how to get the path to that file.

The list of folders in the workspace/project settings are relative (~/path/to/project) so I am struggling to get an absolute path so I can check if the file exists and read it.

Here is what I have so far:

project_configuration_file = sublime.active_window().project_data()["folders"][0]["path"]
        project_configuration_file = Path(project_configuration_file).resolve()


But I get the following error:

FileNotFoundError: [Errno 2] No such file or directory: '/opt/sublime_text/~'

It looks like python is resolving the relative path very wrong …

Can some one tell me how to handle this?

Thank you!



You need to use os.path.dirname(Window.project_file_name()) as the base dir to resolve the relative paths. Alternatively, Window.folders() is a list of absolute project paths.



Thank you, sublime.active_window().folders() did the trick!