0

Below code throw encode error:

import os

path = u"E:\\project\\robot_framework\\第一个测试项目\\logs\\log-20170330-094807.html"

os.system("scp "+path+" root@192.168.0.160:/root/jenkins/workspace/RobotFramework/logs/")

error message:

Traceback (most recent call last):

  File "E:\project\robot_framework\PythonDemo\src\try.py", line 12, in <module>
    os.system("scp "+path+" root@192.168.0.160:/root/jenkins/workspace/RobotFramework/logs/")
UnicodeEncodeError: 'ascii' codec can't encode characters in position 31-37: ordinal not in range(128)
smci
  • 32,567
  • 20
  • 113
  • 146
Alex Bruce
  • 533
  • 2
  • 10
  • 23
  • [This post might help you](http://stackoverflow.com/questions/33294213/how-to-decode-unicode-in-a-chinese-text), it seems like you need to encode and decode it first. – Pike D. Mar 30 '17 at 02:24
  • @PikeD. I've tried encode() decode() unicode(), but no help – Alex Bruce Mar 30 '17 at 02:28

1 Answers1

0

You can try to encode the string

import os

path = u"E:\\project\\robot_framework\\第一个测试项目\\logs\\log-20170330-094807.html".encode("utf-8")
os.system("scp "+path+" root@192.168.0.160:/root/jenkins/workspace/RobotFramework/logs/")

Another way:

import sys
reload(sys)
sys.setdefaultencoding('utf-8')

The above code maybe solve some encoding problems, but the use of sys.setdefaultencoding() is discouraged,

See more details from Unicode HOWTO.

McGrady
  • 10,869
  • 13
  • 47
  • 69
  • E:/绗竴涓祴璇曢」鐩甛logs/log-20170330-094807.html: No such process – Alex Bruce Mar 31 '17 at 03:31
  • @AlexBruce I simplified the path, you should change it to `E:\\project\\robot_framework\\第一个测试项目\\logs\\log-20170330-094807.html` – McGrady Mar 31 '17 at 03:34