I am looking to parse (multiple) .dat blockchain files from a specified folder in to a python script. The aim is to print out each transaction (this is temporary, the plan is to write the human readable values to a database). I can send a link to a gist if more information is needed.
def parseBlockFile(self, blockfile):
print 'Parsing block file: %s\n' % blockfile
with open(blockfile, 'rb') as bf:
self.magic_no = read_uint4(bf)
print 'magic_no:\t0x%8x' % self.magic_no
self.blocksize = read_uint4(bf)
print 'size: \t%u bytes' % self.blocksize
self.blockheader = BlockHeader()
self.blockheader.parse(bf)
print 'Block header:\t%s' % self.blockheader
self.transaction_cnt = read_varint(bf)
print 'Transactions: \t%d' % self.transaction_cnt
self.transactions = []
print 'List of transactions'
for i in range(0, self.transaction_cnt):
tx = Transaction()
tx.parse(bf)
self.transactions.append(tx)
print '='*50
print ' TX NUMBER: %d' % (i+1)
print '='*50
print tx
print '\n'
def parseBlockFile(blockfile):
block = Block()
block.parseBlockFile(blockfile)
if __name__ == "__main__":
import os # Open a file
path = "/Users/user_name/PycharmProjects/block_chain_parse/data/"
dirs = os.listdir(path)
# Find each file in the folder
for file in dirs:
print file #check the file makes it this far
parseBlockFile(file) #pass each file
I get the following error:
blk00000.dat
Parsing block file: blk00000.dat
Traceback (most recent call last):
File "block.py", line 212, in <module>
parseBlockFile(file) #pass each file
File "block.py", line 203, in parseBlockFile
block.parseBlockFile(blockfile)
File "block.py", line 173, in parseBlockFile
with open(blockfile, 'rb') as bf:
IOError: [Errno 2] No such file or directory: 'blk00000.dat'