的第一篇文章中運行時如此善待請,我尋覓了周圍很多,但我發現大多數事情都是相關的Python 2Python3:UnicodeEncodeError只能從crontab中
我有一個Python3腳本,建立從一個zip文件一個文件列表;只有當腳本從crontab運行時,它纔會以UnicodeEncodeError失敗,但是當它從交互式控制檯運行時,它運行得非常完美。 我想環境中一定有東西,但我似乎無法弄清楚什麼。
這是代碼摘錄:
def zipFileList(self, rootfolder, filelist, zip_file, logger):
count = 0
logger.info("Generazione file zip {0}: da {1} files".format(zip_file, len(filelist)))
zip = zipfile.ZipFile(zip_file, "w", compression=zipfile.ZIP_DEFLATED)
for curfile in filelist:
zip.write(os.path.join(rootfolder, curfile), curfile, zipfile.ZIP_DEFLATED)
count = count + 1
zip.close()
logger.info("Scrittura terminata: {0} files".format(count))
而這是該代碼片段的日誌輸出:
2012-07-31 09:10:03,033: root - ERROR - Traceback (most recent call last):
File "/usr/local/lib/python3.2/zipfile.py", line 365, in _encodeFilenameFlags
return self.filename.encode('ascii'), self.flag_bits
UnicodeEncodeError: 'ascii' codec can't encode characters in position 56-57: ordinal not in range(128)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "XBE.py", line 45, in main
pam.executeList(logger)
File "/home/vte/vtebackup/vte41/scripts/ptActivityManager.py", line 62, in executeList
self.executeActivity(act, logger)
File "/home/vte/vtebackup/vte41/scripts/ptActivityManager.py", line 71, in executeActivity
self.exAct_FileBackup(act, logger)
File "/home/vte/vtebackup/vte41/scripts/ptActivityManager.py", line 112, in exAct_FileBackup
ptfs.zipFileList(srcfolder, filelist, arcfilename, logger)
File "/home/vte/vtebackup/vte41/scripts/ptFileManager.py", line 143, in zipFileList
zip.write(os.path.join(rootfolder, curfile), curfile, zipfile.ZIP_DEFLATED)
File "/usr/local/lib/python3.2/zipfile.py", line 1115, in write
self.fp.write(zinfo.FileHeader())
File "/usr/local/lib/python3.2/zipfile.py", line 355, in FileHeader
filename, flag_bits = self._encodeFilenameFlags()
File "/usr/local/lib/python3.2/zipfile.py", line 367, in _encodeFilenameFlags
return self.filename.encode('utf-8'), self.flag_bits | 0x800
UnicodeEncodeError: 'utf-8' codec can't encode character '\udcc3' in position 56: surrogates not allowed
這是crontab的線:
10 9 * * * /home/vte/vtebackup/vte41/scripts/runbackup.sh >/dev/null 2>&1
而這是runbackup.sh的內容:
#! /bin/bash -l
cd /home/vte/vtebackup/vte41/scripts
/usr/local/bin/python3.2 XBE.py
爲例外情況是始終不變的,但它似乎並沒有包括任何非ASCII字符的文件:
/var/vhosts/vte41/http_docs/vtecrm41/storage/2012/July/week4/169933_Puccini_Gabriele.tif
操作系統是Ubuntu Linux操作系統的10.04 LTS,Python版本3.2(安裝側作爲altinstall與其他Python版本一起)。 所有Python源文件有這個家當
#!/usr/bin/env python3.2
爲第一行
你能不能幫我找到了什麼問題以及如何解決這個問題?
對於當zip文件試圖編碼的文件名嵌入信息在它的一個未知的原因,文件名具有[Unicode代理](http://www.htmlescape.net/dc/unicode_char_dcc3的.html)。也許操作系統的問題?你可以在腳本中記錄'curfile'嗎? – CharlesB 2012-07-31 08:03:45