Sublime Forum

Path problem when opening files

#1

Hi all,

I’m currently working on a sublime text 2 plugin and I’m having problems with opening files.
When I mean opening files, I don’t mean opening files inside the sublime editor or anything. What I mean is for a Python module to programmatically open a file from the file system to read from it.

First of all, my plugin is based on cleanjs (github.com/captainbrosset/cleanjs/) a python command line tool I’m working on. The plugin itself is just another way of accessing cleanjs rather than from the command line. Therefore I can’t rely on the normal SublimeText configuration system.

Say I have a structure like this:

  • cleanjs
    |–file.txt
    |–configreader.py
    |–cleanjs_commandline.py
    |–cleanjs_sublime.py

Say both cleanjs_commandline.py and cleanjs_sublime.py need to read the file.txt using the configreader.py module.
configreader.py cannot make any assumption if it’s being called in normal command line mode or through the sublime plugin.
In command line mode, a simple open(“file.txt”) works very well, but it fails completely when the it’s executed from the sublime text plugin.

I guess this comes from the way sublime imports and run plugins of course, so the path looks different.

If I do some dirty if statements, I will be able to include the proper file anyway, but I was hoping there would be a nicer way of opening files easily, no matter where the .py module gets called from and how, just knowing the relative path of that file.

By the way, this is not only linked to Sublime, it does the same in Python depending on how I execute my program.

Thanks for your help!

0 Likes

#2
open("file.txt")

It is relative path to current working dir.

What happened if you use absolute path?

Any exception? IOError file not found?

0 Likes

#3

Hi,
Using the os.getcwd() I found out that sublime plugins were run in the / working directory, basically at the root of the system. This is why my file was not getting found.
In order to make it work, I now have my sublime plugin pass to cleanjs the package directory using sublime.packages_path()
So everything’s fine now.

0 Likes