Home Download Buy Blog Forum Support

Path problem when opening files

Path problem when opening files

Postby patrickbrosset on Wed Feb 15, 2012 8:01 am

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 (https://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!
patrickbrosset
 
Posts: 7
Joined: Sat Jan 21, 2012 7:20 am

Re: Path problem when opening files

Postby Cjkjvfnby on Wed Feb 15, 2012 11:32 am

Code: Select all
open("file.txt")

It is relative path to current working dir.

What happened if you use absolute path?

Any exception? IOError file not found?
Cjkjvfnby
 
Posts: 20
Joined: Wed Feb 01, 2012 11:35 am

Re: Path problem when opening files

Postby patrickbrosset on Wed Feb 15, 2012 7:45 pm

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.
patrickbrosset
 
Posts: 7
Joined: Sat Jan 21, 2012 7:20 am


Return to Plugin Development

Who is online

Users browsing this forum: Yahoo [Bot] and 5 guests