gpt4 book ai didi

python 3 : UnicodeEncodeError only when run from crontab

转载 作者:太空狗 更新时间:2023-10-30 00:21:41 24 4
gpt4 key购买 nike

第一次发帖,请多多关照,我搜索了很多,但我发现的大多数内容都与 Python 2 相关。

我有一个 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 LTS 10.04,Python 版本 3.2(作为 altinstall 与其他 Python 版本并排安装)。所有 Python 源文件都有这个 shebang

#!/usr/bin/env python3.2

第一行

你能帮我找出问题所在以及如何解决这个问题吗?

最佳答案

一名团队成员在 Python bug thread 中找到了解决方案.

通过在脚本命令前添加 LANG 指令解决了这个问题:

* * * * * LANG=it_IT.UTF-8 /home/vte/vtebackup/vte41/scripts/runbackup.sh >/dev/null 2>&1

我希望这对其他人有用,因为我自己在这方面摸不着头脑:)

关于 python 3 : UnicodeEncodeError only when run from crontab,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11735363/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com