바이너리 파일을 작성하는 두 가지 방법이 있습니다. 첫째는 파일 업로드 (즉, enctype = "multipart/form-data"형식의 핸들링)에 해당하는 서버가받은 데이터로 작동하고 두 번째 방법은 파일 전자 메일 첨부 파일 (즉, get_payload()를 사용하여 전자 메일 메시지 본문을 구문 분석하여 얻은 파일 데이터)로 보낸 데이터.Python IO 전문가 :이 두 방법의 차이점은 무엇입니까?
이상한 점은 교환 할 수 없다는 것입니다. 이메일 첨부 파일에서 파싱 된 데이터를 저장하기 위해 첫 번째 파일을 사용하면 실패합니다. 마찬가지로 업로드 된 파일 데이터를 처리 할 때 두 번째 함수가 실패합니다.
중요한 차이점은 무엇입니까?
def write_binary_file (folder, filename, f, chunk_size=4096):
"""Write the file data f to the folder and filename combination"""
result = False
if confirm_folder(folder):
try:
file_obj = open(os.path.join(folder, file_base_name(filename)), 'wb', chunk_size)
for file_chunk in read_buffer(f, chunk_size):
file_obj.write(file_chunk)
file_obj.close()
result = True
except (IOError):
print "file_utils.write_binary_file: could not write '%s' to '%s'" % (file_base_name(filename), folder)
return result
이 두번째 방법 :
def write_binary_file (folder, filename, filedata):
"""Write the binary file data to the folder and filename combination"""
result = False
if confirm_folder(folder):
try:
file_obj = open(os.path.join(folder, file_base_name(filename)), 'wb')
file_obj.write(filedata)
file_obj.close()
result = True
except (IOError):
print "file_utils.write_binary_file: could not write '%s' to '%s'" % (file_base_name(filename), folder)
return result
그것은 더 도움이 될 수 있습니다. 또한 read_buffer는 정확히 무엇입니까? – JimB