給出的簡單腳本:爲什麼Python3解釋器僅在通過fcron執行時才引發UnicodeEncodeError?
#!/usr/bin/env python3
b = 'строка'.encode()
print(b.decode('utf-8'))
如果我直接作爲python3 script.py
或/full/path/to/script.py
或間接地運行它通過的crontab(例如0 0 * * * /full/path/to/script.py
),然後它的正常執行(沒有錯誤)。但是,當它通過fcrontab執行鍼對同一$ USER(用同樣的工作0 0 * * * /full/path/to/script.py
)那麼Python 3.5.2引發異常:
Traceback (most recent call last):
File "/full/path/to/script.py", line 4, in <module>
print(b.decode('utf-8'))
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2: ordinal not in range(128)
豈是可能的嗎?爲什麼Python嘗試將字節解碼爲'ascii'而不是'utf-8'?
看到這裏https://wiki.python.org/moin/PrintFails –
哦,它是真正的問題與設定給fcron因爲現場的只有'print'功能(在我的情況fcron有locales'POSIX'而不是* .UTF-8)...謝謝! – kupgov