Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python中的UnicodeDecodeError消息_Python - Fatal编程技术网

python中的UnicodeDecodeError消息

python中的UnicodeDecodeError消息,python,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") fo

我对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>
回溯(最近一次呼叫最后一次):
文件“C:\Documents and Settings\hp\Desktop\my\u python\u files\AU20-10297-2\u yield\u 69p4\u 11Failures\u 2\u 10\u 14python\exec.py”,第8行,在
shutil.copyfileobj(g,f)
copyfileobj中的第68行文件“C:\Python33\lib\shutil.py”
buf=fsrc.read(长度)
文件“C:\Python33\lib\encodings\cp1252.py”,第23行,解码
返回编解码器.charmap\u解码(输入、自身错误、解码表)[0]
UnicodeDecodeError:“charmap”编解码器无法解码位置754中的字节0x8d:字符映射到

有人能帮我修复代码,使其更加健壮吗?

您已经以文本模式打开了文件,这意味着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)

谢谢你解决了我的问题!