← Back to team overview

sikuli-driver team mailing list archive

Re: [Question #132445]: Reading / writing to a file in sikuli

 

Question #132445 on Sikuli changed:
https://answers.launchpad.net/sikuli/+question/132445

Alex Nilsson posted a new comment:
@RaiMan

___________________________________________________

--- The write to file feature in sikuli seems to be a little buggy
What do you mean by that?

and:

So it would help, to have a snippet, that you think it produces your observed problems.
___________________________________________________

Well, I copy/pasted the example above.

my_dir = "c:\\Users\\yourName\\.....\\"
el = "\n" # the end of line you want to use
my_file=file(my_dir + "mynewfile.txt", "w")
my_file.write("line"+el) # if you want to have line breaks
my_file.close


The only 2 things I changed (Besides changing my_dir to a valid path) was:
my_file=file(my_dir + "mynewfile.txt", "w") to my_file=file(my_dir + "mynewfile.txt", "a")

So it would append to the file, instead of overwriting it.

And my_file.write("line"+el) to:
my_file.write("line1"+el)
my_file.write("line2"+el)
my_file.write("line3"+el)

It works the first time, creating a file called mynewfile.txt
containing:

line1
line2
line3

Afterwards it returns me to the sikuli gui.

Now I ran the script one more time, expecting the file would contain:

line1
line2
line3
line1
line2
line3

But I had no such luck, it's like sikuli still has the file open, from
the first time the script ran..so the file is "in-use" and can't be
written to.

Now if you run the script multiple times, about 50% of the times it'll write the text to the file..so I'm guessing, that if you put in a delay before accessing the file again or somehow get the file to report back, when the script releases it, it'll work..
___________________________________________________

Writing to files using file/write/close has nothing to do with Sikuli as such, these are built-in features of the underlying Jython, the interpreter that runs the Sikuli scripts in the IDE.
___________________________________________________

Sorry for being impresice, it was just easyer to write ;-)

-- 
You received this question notification because you are a member of
Sikuli Drivers, which is an answer contact for Sikuli.