手工执行python3程序没问题,放在cron里面就不执行有中文的代码,高手帮忙啊。急!
先介绍下基本情况
环境: redhat Python3.2
目的:定时执行python程序
问题:手工执行python程序没有问题,放到crontab列表里面执行的时候,遇到中文就抛异常
python程序源码
#!/usr/local/bin/python3#coding=gbkimport io, sys, time, re, osdef main(): try: print("a") print("中") print("b") except Exception as e: print("Exception: " + str(e))if __name__ == '__main__': main()
[kkk@vs ~]$ python3 /home/kkk/9/app/tw/tw3.pya中b
[kkk@vs ~]$ crontab -l*/1 * * * * /usr/local/bin/python3 /home/kkk/9/app/tw/tw3.py > cronlog5
[kkk@vs ~]$ more cronlog5aException: 'ascii' codec can't encode character '\u4e2d' in position 0: ordinal not in range(128)
import sys reload(sys)sys.setdefaultencoding('utf8')
[kkk@vs ~]$ pythonPython 2.4.3 (#1, Sep 21 2011, 20:06:00) [GCC 4.1.2 20080704 (Red Hat 4.1.2-51)] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> import sys>>> sys.getdefaultencoding() 'utf-8'>>>