Chilkat2-Python
Chilkat2-Python
File Read Blocks
Demonstrates how to read a file in fixed-size blocks (except for the very last block).Chilkat Chilkat2-Python Downloads
import sys
import chilkat2
success = False
# Demonstrates how to read a file in blocks,
# which can be useful when uploading to cloud storage
# services such as Azure, S3, Google, etc.
# For this example, we're simply writing the blocks
# to an output file, and then checking to see if the
# resulting file contents equals the original file contents.
facSrc = chilkat2.FileAccess()
facDest = chilkat2.FileAccess()
srcPath = "qa_data/xml/hamlet.xml"
destPath = "qa_output/hamletOut.xml"
success = facSrc.OpenForRead(srcPath)
success = facDest.OpenForWrite(destPath)
# Assuming success for the example..
# How many 1024-byte blocks? (Including 1 for the last partial block)
numBlocks = facSrc.GetNumBlocks(1024)
i = 0
while (i < numBlocks) :
# dataBlock is a memoryview
dataBlock = facSrc.ReadBlock(i,1024)
if (facSrc.LastMethodSuccess != True):
print(facSrc.LastErrorText)
sys.exit()
success = facDest.FileWrite(dataBlock)
if (success != True):
print(facDest.LastErrorText)
sys.exit()
i = i + 1
facSrc.FileClose()
facDest.FileClose()
bEqual = facSrc.FileContentsEqual(srcPath,destPath)
if (bEqual != True):
print("Something went wrong!")
sys.exit()
print("File successfully copied by blocks.")