python中的UnicodeDecodeError消息

时间:2014-02-15 19:10:53

标签: python

我对Python编程比较陌生。我在Windows XP上使用Python 3.3.2。

我的程序正在运行,然后我突然收到了UnicodeDecodeError错误消息。

exec.py文件如下所示:

import re
import os,shutil  
f=open("C:/Documents and Settings/hp/Desktop/my_python_files/AU20-10297-2_yield_69p4_11fails_2_10_14python/a1.txt","a")
for r,d,fi in os.walk("C:/Documents and Settings/hp/Desktop/my_python_files/AU20-10297-2_yield_69p4_11fails_2_10_14python"):
for files in fi:
    if files.startswith("data"):
        g=open(os.path.join(r,files))
        shutil.copyfileobj(g,f)
        g.close()
f.close()

keywords = ['FAIL']
pattern = re.compile('|'.join(keywords))



inFile = open("a1.txt")
outFile =open("failure_info", "w")
keepCurrentSet = False
for line in inFile:
if line.startswith("                                 Test Results"):
    keepCurrentSet = False

if keepCurrentSet:
    outFile.write(line)

if line.startswith("Station ID "):
    keepCurrentSet = True

#if 'FAIL' in line in inFile:
   # outFile.write(line)

if pattern.search(line):
    outFile.write(line)

inFile.close()
outFile.close()

现在,a1.txt最初是一个空的种子文本文件,用于从数据文件中收集数据。 我收到以下错误消息:

Traceback (most recent call last):
  File "C:\Documents and Settings\hp\Desktop\my_python_files\AU20-10297-2_yield_69p4_11fails_2_10_14python\exec.py", line 8, in <module>
    shutil.copyfileobj(g,f)
  File "C:\Python33\lib\shutil.py", line 68, in copyfileobj
    buf = fsrc.read(length)
  File "C:\Python33\lib\encodings\cp1252.py", line 23, in decode
    return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x8d in position 754: character maps to <undefined>

任何人都可以帮我修复代码,使其更强大吗?

1 个答案:

答案 0 :(得分:1)

您已在文本模式下打开文件,这意味着Python将尝试将内容解码为Unicode。您通常需要为文件指定正确的编解码器(或者Python将使用您的平台默认值),但这里只是使用shutil.copyfileobj()复制文件,并且不需要解码。

改为以二进制模式打开文件。

f = open(..., 'ab')
for r,d,fi in os.walk(...):   
    for files in fi:  
        if files.startswith("data"):
            g = open(os.path.join(r, files), 'rb')
            shutil.copyfileobj(g,f)

请注意将b添加到文件模式。

您可能希望将文件对象用作上下文管理器,以便它们自动关闭:

with open(..., 'ab') as outfh:
    for r,d,fi in os.walk(...):   
        for files in fi:  
            if files.startswith("data"):
                with open(os.path.join(r, files), 'rb') as infh:
                    shutil.copyfileobj(infh, outfh)