Python module sys in sikuli does not work

Asked by xseniya on 2018-10-03

hello!
help me please...

my sikuli script:

import os
import sys

logfile = open(sys.path[0] + 'LOGtest.txt', 'w')
logfile.write('Test_Ok123')
logfile.close()

when i try do it in sikuli IDE - it work ok and logfile create
but when i try do it in pyhton like this:

 subprocess.call(['C:\\Program Files (x86)\\Sikuli X\\runsikulix.cmd' , '-r', scenario])

logfile(LOGtest.txt) do not create

that is it working only in sikuli IDE

why? and how i can do it?

thank you in advance

Question information

Language:
English Edit question
Status:
Solved
For:
Sikuli Edit question
Assignee:
No assignee Edit question
Solved by:
xseniya
Solved:
2018-10-04
Last query:
2018-10-04
Last reply:
2018-10-03
xseniya (xseniya) said : #1

when i do absolutly path it working everywhere

RaiMan (raimund-hocke) said : #2

--- about logging:
https://sikulix-2014.readthedocs.io/en/latest/scripting.html#writing-and-redirecting-log-and-debug-messages

--- to build an absolute path relative to your script:
os.path.join(getBundlepath(), "somename.txt") # in the folder .sikuli
os.path.join(getParentpath(), "somename.txt") # in the folder containing the .sikuli

--- to run other scripts from one main script
https://sikulix-2014.readthedocs.io/en/latest/scripting.html#running-scripts-and-snippets-from-within-other-scripts-and-run-scripts-one-after-the-other

xseniya (xseniya) said : #3

i dont need redirecting sikuli log, i want the sikuli srcipt create txt file and write my text into it

RaiMan (raimund-hocke) said : #4

There is a userlog feature.

But if want to invent your own logging:
the other things should help.

All the best.

xseniya (xseniya) said : #5

no, no..... not a userlog feature, not a logging!

problem is it path.
if i write absolutly path: open('D:\\test\\my_txt.txt', 'w') script worked without IDE, i call it from python.
but if i write: my_txt = open(sys.path[0] + 'my_txt.txt', 'w')

why when i use sys.path my script dont work without IDE?

RaiMan (raimund-hocke) said : #6

Simply: do not rely on sys.path[0] !

Use other features (as mentioned) to get your needed pat consistently.

xseniya (xseniya) said : #7

sys.path dont work in it?
its wery bad...it's sad for me

thank you for taking the time for my question

Manfred Hampl (m-hampl) said : #8

What are the contents of your PATH setting? In many cases the first part is a system directory (without write rights for users).
Does the first part of PATH have a directory separator at the end? If not, you have to add it manually or use os.path.join.

RaiMan (raimund-hocke) said : #9

I did not say: sys.path as such is not working - of course it does!

... but the content of sys.path [0] with SikuliX might Not be what you expect in all cases.

But I am sure you will find a Solution.

xseniya (xseniya) said : #10

exist a python script that runs a sikuli script that performs certain actions and writes the text in the file.txt, that the python then reads.
All of them are stored in the same special folder with read and write access.
If I write to sikuli ".\\ file" that he creates it in the working directory of sikuli (program files(86)) but I don’t need it, I want the file to be created in the folder where it is stored

in python this method works great and sikuli IDE works too.
but i dont understand, why it dont work if sikuli script started .bat-file or python-script

xseniya (xseniya) said : #11

its happened!

solution is:
my_file = open(os.path.join(getParentPath()) + '\\' + 'test.txt', 'w')
my_file .write('Test_Ok_mother fucker')
my_file .close()

by RaiMan

thank all, i'm happy :)