1

I have a Python/Sikuli script where I need to store the location of the file in a variable.

I have the IDE on one location, and the file is stored in a different directory.
IDE: C:\Sikuli\runIDE.cmd
File: C:\Script\Files\HelloWorld2.sikuli\HelloWorld.py

Now I use os.getcwd() to fetch the location of the IDE.
But how do I return the full directory path of the file "HelloWorld.py"?

I also used os.path.abspath("HelloWorld2.py") and os.path.dirname(os.path.realpath('HelloWorld2.py')).
But also that didn't work.
Can anyone help me with this?

xiº
  • 4,605
  • 3
  • 28
  • 39
Tenzin
  • 2,415
  • 2
  • 23
  • 36
  • 1
    duplicate (or at least related): [http://stackoverflow.com/questions/7162366/get-location-of-the-py-source-file](http://stackoverflow.com/questions/7162366/get-location-of-the-py-source-file) – alcedine Apr 21 '15 at 10:04
  • Did you try `os.path.realpath(__file__)` ? There are many SO posts about that; the `__file__` attribute is the pathname of the file from which the module was loaded, if it was loaded from a file. – 7hibault Apr 21 '15 at 10:07

2 Answers2

3

In regular Python, using __file__ returns the full path of the directory that contains the executed file:

os.path.dirname( os.path.abspath( __file__ ) )

However, in Sikuli, the __file__ reference is undefined:

from sikuli import *
import os

print( os.path.dirname( os.path.abspath( __file__ ) ) )

When run in Sikuli v2.0.4, we see that __file__ is unsupported:

[error] script [ test2 ] stopped with error in line 6
[error] NameError ( name '__file__' is not defined )
[error] --- Traceback --- error source first
line: module ( function ) statement 
6: main (  <module> )     print( os.path.dirname( os.path.abspath(__file__) ) )
[error] --- Traceback --- end --------------

Instead, use getBundle(), such as:

from sikuli import *
import os

print( os.path.dirname( getBundlePath() ) )
Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
Jerzy Pawlikowski
  • 1,751
  • 19
  • 21
  • @tenzin Or this one http://stackoverflow.com/questions/16771894/python-nameerror-global-name-file-is-not-defined but dirname will return the path of the directory (not the path of the executed file) though – 7hibault Apr 21 '15 at 10:17
3

Sikuli seems to have some issue's with the __file__
What does work with Sikuli and Python is:

os.path.dirname(getBundlePath()) 

Edit for the question below:
To get the names of the files that are in the directory you can do:

path = os.path.dirname(getBundlePath()) 
directory = os.listdir(path) 
for file in directory: 
    print(file)
Tenzin
  • 2,415
  • 2
  • 23
  • 36
  • But this unfortunately gets the parent directory of the `myscript.sikuli` directory. How do you get the full path to files you put *inside* the `.sikuli` script directory? – jhabbott Jan 19 '16 at 03:23
  • @jhabbott, I added a example code to the answer that would do the trick.To get the complete path for every file, you can make a string and add `path` and `file` to the string. – Tenzin Jan 19 '16 at 17:04
  • That lists the files in the same directory as the script-directory, most likely other unrelated scripts. eg. if I keep my scripts in `/home/joe/sikuliscripts/` and one bundle is `script1.sikuli` - then `getBundlePath()` within that script gives `/home/joe/sikuliscripts/` whereas what I want is `/home/joe/sikuliscripts/script1.sikuli` - I think this is a flaw in Sikuli. – jhabbott Jan 19 '16 at 17:36