Problem to write a txt file

Asked by Leonardo Carvalho Sobral dos Santos

Hi,

I'm having problems to export some vetor values in some txt.
--------------######-----------------

My code :

def testVet(lixeira,vet,TAM):

 # try:
     for i in range(TAM):
        while exists ("1399467003216.png"):
                if exists("1399467003216.png"):

                    dragDrop("1399467074425.png","1399482760598.png")
                    #"1399482760598.png""1399473319344.png""1399471289407.png"
                    lixeira +=1

                else:
                    print("lixeira cheia")

        #print ("Lixeira cheia")
                    print ("quantidade de lixo = "+ str (lixeira))
        vet[i] = lixeira
        return lixeira,vet,TAM

    #except:
       # raise exception

lixeira,vet,TAM=testVet(lixeira,vet,TAM)

print ("quantidade de lixo = "+ str (lixeira))
print (vet)

f = open("C:\\Users\\vntless\\Desktop\\Projetos\\Sikuli_Projeto Automação\\Vetor.txt", 'a')
#f = open("E:\\logfile.log", "a")
f.write(vet)
f.close()
-----------------#######--------------

Current error

[error] script [ vetortest ] stopped with erro

r in line 38
[error] IOError ( (13, 'Permission denied', 'C:\\Users\\vntless\\Desktop\\Projetos\\Sikuli_Projeto Automa\xc3\xa7\xc3\xa3o\\Vetor.txt') )

----------#############-----------

More info:

OS :Windows 8.1
Sikuli Version :1.0.1

Question information

Language:
English Edit question
Status:
Answered
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
Leonardo Carvalho Sobral dos Santos (leonardocsobral) said :
#1

I did in different way... but still return error

New code

text=vet
f = open("Vetor.txt", 'w')
#C:\\Users\\vntless\\Desktop\\Projetos\\Sikuli_Projeto Automação\\Vetor.txt
#f = open("E:\\logfile.log", "a")
f.write(text)
f.close()

Error.................

quantidade de lixo = 2
[2, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[error] script [ vetortest ] stopped with error in line 41
[error] TypeError ( argument 1 must be string or read-only character buffer, not list )

Revision history for this message
Eugene S (shragovich) said :
#2

Hi,

I think that the issue in your "New code" is that you are trying to write a list type variable to a file. If that's what you are trying to do, try to use "writelines" instead of write:

f.writelines(text)

As for your initial code, try to change the path to include only English characters. I believe that to be an encoding issue.

Cheers,
Eugene

Revision history for this message
RaiMan (raimund-hocke) said :
#3

if you want to have
[2, 0, 0, 0, 0, 0, 0, 0, 0, 0]

in your file
f.write(str(text))

this is what print does internally automatically (converting everything into it's string representation if it is not a string.

If you want to have each value on a separate line, then writelines() will not work in this case, since your vector does not contain strings, but numbers. Moreover: writelines does not automatically add newlines.

this would write each value on a separate line:
f.writelines([str(x) + "\n" for x in text])

[str(x) + "\n" for x in text] is a so called list-comprehension, which on the fly creates a new list based on the given receipt.

Can you help with this problem?

Provide an answer of your own, or ask Leonardo Carvalho Sobral dos Santos for more information if necessary.

To post a message you must log in.