python code We're using structs and focusing on binary file I/O def read_attributes(file_path, format): """ Given a binary file written using struct.pack() with the given format, return the number of bytes in the file and the number of bytes in each chunk. :param file_path: :param format: a string with the format of the data in the struct :return: tuple with the number of bytes in file and the number of bytes written in each chunk """
Here is the solution to above problem. The chunk size is taken up to be 1024 Bytes or 1KB . In the above problem we need to open the file as a binary file and read 1024 bytes at a time.
Python code
def read_attributes(file_path, Format):
Chunk=0 #number of chunks
Bytes=0
with open(fileName, Format) as fin:
bytesRead = fin.read(1024) #1024 is the chunk size
Chunk=Chunk+1
for byte in bytesRead:
Bytes=Bytes+1;
return (Bytes,Chunk)
fileName = raw_input("Enter a file name:")
read_attributes(fileName,"rb")
Screenshot of python code ( For indentation help)
Get Answers For Free
Most questions answered within 1 hours.