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
There error I am getting is as follows:
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'
Any thoughts?
You could use glob
, like so:
from glob import glob
if __name__ == "__main__":
path = "H:/Workspaces_Python/test/"
files = glob(path + "*.dat")
for file in files:
print(file)
parseBlockFile(file) #pass each file
Are you running the code from the same directory as the .dat files? Try adding the path when calling parseBlockFile
:
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(path+file) #pass each file
os.listdir(path)
return names, not fullpaths
change parseBlockFile(file)
to
fullpath = os.path.join(path,file)
if os.path.isfile(fullpath) and fullpath.endswith('.dat'):
parseBlockFile(fullpath)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.