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)
ERROR :
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
TKS, 레나 - 예, 이것은 내가 필요한 비밀 소스이었다. 파이썬 3을 처음 접하십니까? – DrLou
실제로 3 개의 특정 파이썬이 아닙니다. 바이너리 파일은 Python 2에서도 'b'플래그로 열어야한다. –
그래, 다 회상에서 나에게 다소 바보 같이 보인다 -하지만 이것이 우리가 바보를 배우는 방법이다! 당신은 아마 생각하고 있습니다 : RTFM! 도움에 다시 한번 감사드립니다. – DrLou