如何从文件中读取文件内容?

DrL*_*Lou 5 python os.walk python-3.x

使用Python3,希望os.walk文件目录,将它们读入二进制对象(字符串?)并对它们进行进一步处理.不过第一步:如何读取文件的结果os.walk

# NOTE: Execute with python3.2.2

import os
import sys

path = "/home/user/my-files"

count = 0
successcount = 0
errorcount = 0
i = 0

#for directory in dirs
for (root, dirs, files) in os.walk(path):
 # print (path)
 print (dirs)
 #print (files)

 for file in files:

   base, ext = os.path.splitext(file)
   fullpath = os.path.join(root, file)

   # Read the file into binary? --------
   input = open(fullpath, "r")
   content = input.read()
   length = len(content)
   count += 1
   print ("    file: ---->",base," / ",ext," [count:",count,"]",  "[length:",length,"]")
   print ("fullpath: ---->",fullpath)
Run Code Online (Sandbox Code Playgroud)

错误:

Traceback (most recent call last):
  File "myFileReader.py", line 41, in <module>
    content = input.read()
  File "/usr/lib/python3.2/codecs.py", line 300, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xe2 in position 11: invalid continuation byte
Run Code Online (Sandbox Code Playgroud)

Len*_*bro 9

要读取二进制文件,必须以二进制模式打开文件.更改

input = open(fullpath, "r")
Run Code Online (Sandbox Code Playgroud)

input = open(fullpath, "rb")
Run Code Online (Sandbox Code Playgroud)

read()的结果将是一个bytes()对象.