double backslashes in printed strings --- is by convention (a \ must be escaped with a \ )

Asked by furni

when i write code below , i'm getting the path including double backslashes . How do I get rid of extra backslash?
############################
import sys
import codecs
print sys.path
sys.path.append("C:\Program Files\SikuliX\lib.sikuli")
print sys.path
path1 = u"Users\\system99\\Desktop\\sikuli_func.txt".encode("utf-8")
print path1
for list in open(path1,"r"):
    print list
############################

and i get below in the message box.
############################
u'C:\\Program Files\\SikuliX\\lib.sikuli', u'C:\\Program Files\\SikuliX\\lib.sikuli', u'C:\\Program Files\\SikuliX\\lib.sikuli', u'C:\\Program Files\\SikuliX\\lib.sikuli', 'C:\\Program Files\\SikuliX\\lib.sikuli']
Users\system99\Desktop\sikuli_func.txt

[error] script [ temp6 ] stopped with error in line 8
[error] IOError ( (2, 'No such file or directory', 'Users\\system99\\Desktop\\sikuli_func.txt') )
############################

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
furni
Solved:
Last query:
Last reply:
Revision history for this message
RaiMan (raimund-hocke) said :
#1

The double \ are only in the printout (according to the need to escape a \ with the escape character \). I guess this is to be prepared for copy&paste. The strings themself internally are correct.

why do you think, you need that?
path1 = u"Users\\system99\\Desktop\\sikuli_func.txt".encode("utf-8")

path1 = r"Users\system99\Desktop\sikuli_func.txt"
should be sufficient.

your problem:
[error] IOError ( (2, 'No such file or directory', 'Users\\system99\\Desktop\\sikuli_func.txt') )

a relative path is looked for in the current working directory, but I guess you mean:
path1 = r"c:\Users\system99\Desktop\sikuli_func.txt"

Revision history for this message
furni (dfhfour) said :
#2

Thanks for answering, Raiman!
>The double \ are only in the printout (according to the need to escape a \ with the escape character \). I guess this is to be >prepared for copy&paste. The strings themself internally are correct.
>
>why do you think, you need that?

I'm really new to Sikuli or python, and thought the backslash was mandatory.

>path1 = u"Users\\system99\\Desktop\\sikuli_func.txt".encode("utf-8")
>path1 = r"Users\system99\Desktop\sikuli_func.txt"
>should be sufficient.
>
>your problem:
>[error] IOError ( (2, 'No such file or directory', 'Users\\system99\\Desktop\\sikuli_func.txt') )
>
>a relative path is looked for in the current working directory, but I guess you mean:
>path1 = r"c:\Users\system99\Desktop\sikuli_func.txt"

I changed the path1 to path1 = r"c:\Users\system99\Desktop\sikuli_func.txt", and that prblem was solved .
But when there are chinese characters in the path, still get an error. :((

###########################
import sys
import codecs
path1 = u"D:\\依頼分\\sikuli.funcあ.txt".encode("utf-8")
print path1
for list in codecs.open(path1,"r","shift_jis"):
    print list.encode("utf-8")
###########################

and i get the error below

###########################
D:\依頼分\sikuli.funcあ.txt
[error] script [ temp6 ] stopped with error in line 5
[error] IOError ( (2, 'No such file or directory', 'D:\\\xe4\xbe\x9d\xe9\xa0\xbc\xe5\x88\x86\\sikuli.func\xe3\x81\x82.txt') )
[error] --- Traceback --- error source first line: module ( function ) statement 881: codecs ( open ) IOError: (2, 'No such file or directory', 'D:\\\xe4\xbe\x9d\xe9\xa0\xbc\xe5\x88\x86\\sikuli.func\xe3\x81\x82.txt')
[error] --- Traceback --- end --------------

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

this works with version 1.1.0 on Windows 10 64 with Java 8 (should work on Win7, 8 and Java 7 too):

data = u"c:\SikuliX\依頼分\sikuli.funcあ.txt"
dir = os.path.dirname(data)
uprint("*** content of " + dir)
for fname in os.listdir(dir):
  uprint(fname)
uprint("*** content of " + data)
ftxt = open(data)
for line in ftxt.readlines():
  uprint(line.strip())

... and prints out:
*** content of c:\SikuliX\依頼分
sikuli.funcあ.txt
*** content of c:\SikuliX\依頼分\sikuli.funcあ.txt
sikuli.funcあ line 1
sikuli.funcあ line 2
sikuli.funcあ line 3

Revision history for this message
furni (dfhfour) said :
#4

Thanks for such a quick reply!! :)))

but I still get errors below :(

############################
data = u"C:\Users\aaaaa\Desktop\依頼文\sikuli_funcあ.txt"
dir = os.path.dirname(data)
uprint("*** content of " + dir)
for fname in os.listdir(dir):
  uprint(fname)
uprint("*** content of " + data)
ftxt = open(data)
for line in ftxt.readlines():
  uprint(line.strip())
############################

and print out..

############################

[error] script [ third ]] stopped with error at line --unknown--
[error] Could not evaluate error source nor reason. Analyze StackTrace!
[error] java.lang.IllegalArgumentException: Cannot create PyString with non-byte value
at org.python.core.PyString.<init>(PyString.java:64)
at org.python.core.PyString.<init>(PyString.java:70)
at org.python.core.Py.UnicodeDecodeError(Py.java:357)
at org.python.core.codecs.decoding_error(codecs.java:1601)
at org.python.core.codecs.insertReplacementAndGetResume(codecs.java:1573)
at org.python.core.PyString.hexescape(PyString.java:537)
at org.python.core.PyString.decode_UnicodeEscape(PyString.java:465)
at org.python.antlr.GrammarActions.extractString(GrammarActions.java:535)
at org.python.antlr.GrammarActions.extractStrings(GrammarActions.java:468)
at org.python.antlr.PythonParser.atom(PythonParser.java:10996)
at org.python.antlr.PythonParser.power(PythonParser.java:10363)
at org.python.antlr.PythonParser.factor(PythonParser.java:10290)

at org.python.antlr.PythonParser.term(PythonParser.java:9855)
at org.python.antlr.PythonParser.arith_expr(PythonParser.java:9581)
at org.python.antlr.PythonParser.shift_expr(PythonParser.java:9308)
at org.python.antlr.PythonParser.and_expr(PythonParser.java:9141)
at org.python.antlr.PythonParser.xor_expr(PythonParser.java:8978)
at org.python.antlr.PythonParser.expr(PythonParser.java:8814)
at org.python.antlr.PythonParser.comparison(PythonParser.java:8360)
at org.python.antlr.PythonParser.not_test(PythonParser.java:8287)
at org.python.antlr.PythonParser.and_test(PythonParser.java:8073)
at org.python.antlr.PythonParser.or_test(PythonParser.java:7911)
at org.python.antlr.PythonParser.test(PythonParser.java:7771)
at org.python.antlr.PythonParser.testlist(PythonParser.java:12705)
at org.python.antlr.PythonParser.expr_stmt(PythonParser.java:3021)
at org.python.antlr.PythonParser.small_stmt(PythonParser.java:2675)
at org.python.antlr.PythonParser.simple_stmt(PythonParser.jav

a:2524)
at org.python.antlr.PythonParser.stmt(PythonParser.java:2438)
at org.python.antlr.PythonParser.file_input(PythonParser.java:618)
at org.python.antlr.BaseParser.parseModule(BaseParser.java:78)
at org.python.core.CompileMode$3.dispatch(CompileMode.java:22)
at org.python.core.ParserFacade.parse(ParserFacade.java:158)
at org.python.core.ParserFacade.parse(ParserFacade.java:188)
at org.python.core.Py.compile_flags(Py.java:1956)
at org.python.core.__builtin__.execfile_flags(__builtin__.java:527)
at org.python.util.PythonInterpreter.execfile(PythonInterpreter.java:286)
at org.sikuli.scriptrunner.JythonScriptRunner.runPython(JythonScriptRunner.java:208)
at org.sikuli.scriptrunner.JythonScriptRunner.runScript(JythonScriptRunner.java:176)
at org.sikuli.ide.SikuliIDE$ButtonRun$1.run(SikuliIDE.java:2369)

java.lang.IllegalArgumentException: java.lang.IllegalArgumentException: Cannot create PyString with non-byte value
############################

Im running it on Win7 32bit java version "1.8.0_60" Java(TM) SE Runtime Environment (build 1.8.0_60-b27)
Java HotSpot(TM) Client VM (build 25.60-b23, mixed mode, sharing)

Plenty of errors.... I'm confused :(((

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

What SikuliX version?

How do you run the script?

Revision history for this message
furni (dfhfour) said :
#6

The version is sikulixsetup-1.1.0-20151004.231024-183-forsetup. It's Sikuli 1.1.0.

Im running it from Sikuli IDE by typing r+CTRL

When double clicking runsikuli.cmd to start sikuli IDE, i get the message below in the command prompt.

+++ running this Java
java version "1.8.0_60"
Java(TM) SE Runtime Environment (build 1.8.0_60-b27)
Java HotSpot(TM) Client VM (build 25.60-b23, mixed mode, sharing)
+++ trying to run SikuliX
+++ using: -Xms64M -Xmx512M -Dfile.encoding=UTF-8 -Dsikuli.FromCommandLine -jar
C:\Users\aaaaa\Desktop\Sikuli\sikulix.jar
10 10, 2015 9:41:52 午前 java.util.prefs.WindowsPreferences <init>
WARNING: Could not open/create prefs root node Software\JavaSoft\Prefs at root 0
x80000002. Windows RegCreateKeyEx(...) returned error code 5.
[info] HotkeyManager: add Capture Hotkey: CTRL+SHIFT 2 (50, 3)
[info] HotkeyManager: add Abort Hotkey: ALT+SHIFT C (67, 9)
[error] IDE: Remembered window not valid. Going to primary screen

Is this related to the error??

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

no.

please upgrade to final version 1.1.0

Revision history for this message
furni (dfhfour) said :
#8

I upgraded to final version! but still get errors. ...

########

data = u"C:\Users\aaaaa\Desktop\依頼文\sikuli_funcあ.txt"
dir = os.path.dirname(data)
uprint("*** content of " + dir)
for fname in os.listdir(dir):
  uprint(fname)
uprint("*** content of " + data)
ftxt = open(data)
for line in ftxt.readlines():
  uprint(line.strip())

########

and print out

[error] script [ FIRST ]] stopped with error at line --unknown--
[error] Could not evaluate error source nor reason. Analyze StackTrace!
[error] java.lang.IllegalArgumentException: Cannot create PyString with non-byte value
at org.python.core.PyString.<init>(PyString.java:64)
at org.python.core.PyString.<init>(PyString.java:70)
at org.python.core.Py.UnicodeDecodeError(Py.java:357)
at org.python.core.codecs.decoding_error(codecs.java:1601)
at org.python.core.codecs.insertReplacementAndGetResume(codecs.java:1573)
at org.python.core.PyString.hexescape(PyString.java:537)
at org.python.core.PyString.decode_UnicodeEscape(PyString.java:465)
at org.python.antlr.GrammarActions.extractString(GrammarActions.java:535)
at org.python.antlr.GrammarActions.extractStrings(GrammarActions.java:468)
at org.python.antlr.PythonParser.atom(PythonParser.java:10996)
at org.python.antlr.PythonParser.power(PythonParser.java:10363)
at org.python.antlr.PythonParser.factor(PythonParser.java:10290)

at org.python.antlr.PythonParser.term(PythonParser.java:9855)
at org.python.antlr.PythonParser.arith_expr(PythonParser.java:9581)
at org.python.antlr.PythonParser.shift_expr(PythonParser.java:9308)
at org.python.antlr.PythonParser.and_expr(PythonParser.java:9141)
at org.python.antlr.PythonParser.xor_expr(PythonParser.java:8978)
at org.python.antlr.PythonParser.expr(PythonParser.java:8814)
at org.python.antlr.PythonParser.comparison(PythonParser.java:8360)
at org.python.antlr.PythonParser.not_test(PythonParser.java:8287)
at org.python.antlr.PythonParser.and_test(PythonParser.java:8073)
at org.python.antlr.PythonParser.or_test(PythonParser.java:7911)
at org.python.antlr.PythonParser.test(PythonParser.java:7771)
at org.python.antlr.PythonParser.testlist(PythonParser.java:12705)
at org.python.antlr.PythonParser.expr_stmt(PythonParser.java:3021)
at org.python.antlr.PythonParser.small_stmt(PythonParser.java:2675)
at org.python.antlr.PythonParser.simple_stmt(PythonParser.jav

a:2524)
at org.python.antlr.PythonParser.stmt(PythonParser.java:2438)
at org.python.antlr.PythonParser.file_input(PythonParser.java:618)
at org.python.antlr.BaseParser.parseModule(BaseParser.java:78)
at org.python.core.CompileMode$3.dispatch(CompileMode.java:22)
at org.python.core.ParserFacade.parse(ParserFacade.java:158)
at org.python.core.ParserFacade.parse(ParserFacade.java:188)
at org.python.core.Py.compile_flags(Py.java:1956)
at org.python.core.__builtin__.execfile_flags(__builtin__.java:527)
at org.python.util.PythonInterpreter.execfile(PythonInterpreter.java:286)
at org.sikuli.scriptrunner.JythonScriptRunner.runPython(JythonScriptRunner.java:208)
at org.sikuli.scriptrunner.JythonScriptRunner.runScript(JythonScriptRunner.java:176)
at org.sikuli.ide.SikuliIDE$ButtonRun$1.run(SikuliIDE.java:2369)

java.lang.IllegalArgumentException: java.lang.IllegalArgumentException: Cannot create PyString with non-byte value

##########

awwww

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

ok, I have to check.

Revision history for this message
furni (dfhfour) said :
#10

import os
import sys
import codecs
data = u"C:\\Users\\aaaaa\\Desktop\\依頼文\\sikuli_funcあ.txt".encode("utf-8")
print data
dir = os.path.dirname(data)
uprint("*** content of " + dir)
for fname in os.listdir(unicode(dir,"utf-8")):
  uprint(fname)
uprint("*** content of " + data)
for line in codecs.open(data,"r","shift_jis"):
    print line.encode("utf-8")
##################

print out

##################
C:\Users\aaaaa\Desktop\依頼文\sikuli_funcあ.txt
*** content of C:\Users\aaaaa\Desktop\依頼文
FIRST.sikuli
SECOND.sikuli
sikuli_funcあ.txt
新しいテキスト ドキュメント - コピー (2).txt
新しいテキスト ドキュメント - コピー (3).txt
新しいテキスト ドキュメント - コピー (4).txt
新しいテキスト ドキュメント - コピー.txt
新しいテキスト ドキュメント.txt
*** content of C:\Users\aaaaa\Desktop\依頼文\sikuli_funcあ.txt

##################

i did it! but still don't get the content of sikuli_funcあ.txt :((

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

if you go back to my recommendation at comment #3, then vital things you did might be:

import codecs
data = u"c:\SikuliX\依頼分\sikuli.funcあ.txt".encode("utf-8") # no idea, why that should be needed in IDE
dir = os.path.dirname(data)
uprint("*** content of " + dir)
for fname in os.listdir(dir):
  uprint(fname)
uprint("*** content of " + data)
ftxt = codecs.open(data,"r","shift_jis")
for line in ftxt.readlines():
  uprint(line.strip())

Revision history for this message
furni (dfhfour) said :
#12

I got nothing
#########

*** content of c:\SikuliX\依頼分
#########

awww

Revision history for this message
furni (dfhfour) said :
#13

i got an error only by writing #-*-coding:utf-8-*- at the first line in the script

the error message is...
#######

[error] script [ FORTH ]] stopped with error at line --unknown--
[error] Could not evaluate error source nor reason. Analyze StackTrace!
[error] java.lang.IllegalArgumentException: Cannot create PyString with non-byte value
at org.python.core.PyString.<init>(PyString.java:64)
at org.python.core.PyString.<init>(PyString.java:70)
at org.python.core.PySyntaxError.<init>(PySyntaxError.java:28)
at org.python.core.ParserFacade.prepBufReader(ParserFacade.java:343)
at org.python.core.ParserFacade.prepBufReader(ParserFacade.java:293)
at org.python.core.ParserFacade.parse(ParserFacade.java:187)
at org.python.core.Py.compile_flags(Py.java:1956)
at org.python.core.__builtin__.execfile_flags(__builtin__.java:527)
at org.python.util.PythonInterpreter.execfile(PythonInterpreter.java:286)
at org.sikuli.scriptrunner.JythonScriptRunner.runPython(JythonScriptRunner.java:208)
at org.sikuli.scriptrunner.JythonScriptRunner.runScript(JythonScriptRunner.java:176)
at org.sikuli.ide.SikuliIDE$Butt

onRun$1.run(SikuliIDE.java:2369)

java.lang.IllegalArgumentException: java.lang.IllegalArgumentException: Cannot create PyString with non-byte value

Revision history for this message
furni (dfhfour) said :
#14

like you said before, u and .encode("utf-8") of data = u"C:\\Users\\aaaaa\\Desktop\\依頼文\\sikuli_funcあ.txt".encode("utf-8") weren't needed

#########
import os
import sys
import codecs
#data = u"C:\\Users\\aaaaa\\Desktop\\依頼文\\sikuli_funcあ.txt".encode("utf-8")
data = "C:\\Users\\aaaaa\\Desktop\\依頼文\\sikuli_funcあ.txt"
print data
dir = os.path.dirname(data)
uprint("*** content of " + dir)
for fname in os.listdir(unicode(dir,"utf-8")):
  uprint(fname)
uprint("*** content of " + data)
for line in codecs.open(data,"r","shift_jis"):
    print line.encode("utf-8")
#########
print out
#########

C:\Users\aaaaa\Desktop\依頼文\sikuli_funcあ.txt
*** content of C:\Users\aaaaa\Desktop\依頼文
FIRST.sikuli
FORTH.sikuli
SECOND.sikuli
sikuli_funcあ.txt
THIRD.sikuli
新しいテキスト ドキュメント - コピー (2).txt
新しいテキスト ドキュメント - コピー (3).txt
新しいテキスト ドキュメント - コピー (4).txt
新しいテキスト ドキュメント - コピー.txt
新しいテキスト ドキュメント.txt
*** content of C:\Users\aaaaa\Desktop\依頼文\sikuli_funcあ.txt
#########

when writing the script below ..
#########
#coding: utf-8
print [u"こんにちは"]
print [u"こんにちは".encode("utf-8")]
print ["こんにちは"]
#########
i get this
#########

[u'\u3053\u3093\u306b\u3061\u306f']
['\xe3\x81\x93\xe3\x82\x93\xe3\x81\xab\xe3\x81\xa1\xe3\x81\xaf']
['\xe3\x81\x93\xe3\x82\x93\xe3\x81\xab\xe3\x81\xa1\xe3\x81\xaf']

Revision history for this message
furni (dfhfour) said :
#15

hey RaiMan! I got it finally!!!

##########
import os
import sys
import codecs
#data = u"C:\\Users\\aaaaa\\Desktop\\依頼文\\sikuli_funcあ.txt".encode("utf-8")
data1 = u"C:\\Users\\aaaaa\\Desktop\\依頼文\\sikuli_funcあ.txt"
data = r"C:\Users\aaaaa\Desktop\依頼文\sikuli_funcあ.txt"
print data
#dir = os.path.dirname(data)
dir = os.path.dirname(data1)
print [dir]
uprint("*** content of " + dir)
#for fname in os.listdir(unicode(dir,"utf-8")):
for fname in os.listdir(dir):
  uprint(fname)
for line in codecs.open(data1,"r","utf-8"):
    uprint("*** content of ***" + line.encode("utf-8"))
##########
print out
##########

C:\Users\aaaaa\Desktop\依頼文\sikuli_funcあ.txt
[u'C:\\Users\\aaaaa\\Desktop\\\u4f9d\u983c\u6587']
*** content of C:\Users\aaaaa\Desktop\依頼文
FIFTH.sikuli
FIRST.sikuli
FORTH.sikuli
SECOND.sikuli
sikuli_funcあ.txt
THIRD.sikuli
新しいテキスト ドキュメント - コピー (2).txt
新しいテキスト ドキュメント - コピー (3).txt
新しいテキスト ドキュメント - コピー (4).txt
新しいテキスト ドキュメント - コピー.txt
新しいテキスト ドキュメント.txt
*** content of ***部品 type number 年月
##########

when outputing to message box , the characters have to be utf-8 but when handing over the variable to the function, the string has to be unicode.