Pass all dat files in folder to function

+1 vote

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'
Sep 4, 2018 in Blockchain by slayer
• 29,370 points
2,382 views

3 answers to this question.

0 votes

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
answered Sep 4, 2018 by digger
• 26,740 points
0 votes

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
answered Sep 4, 2018 by Yoda
0 votes

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)
answered Sep 5, 2018 by Saygum

Related Questions In Blockchain

0 votes
1 answer

Truffle migrate fails due to missing function, but it exists in node_modules

deployer.deploy(password1, password2, deadline, {value: 100, from: accounts[0]}); ...READ MORE

answered Oct 3, 2018 in Blockchain by digger
• 26,740 points
2,024 views
0 votes
1 answer

How to choose which function is called first in Node Js

You can do something like: infuraProvider.getBlockNumber().then(function(blockNumber) { ...READ MORE

answered Oct 8, 2018 in Blockchain by Omkar
• 69,220 points
620 views
0 votes
1 answer

Hyperledger Fabric: How to access transaction ID in invoke function?

You can access transaction ID in Invoke ...READ MORE

answered Nov 5, 2018 in Blockchain by Omkar
• 69,220 points
2,436 views
–1 vote
1 answer

How to get balance of all accounts in ethereum network using javascript?

You can write a function that will ...READ MORE

answered Jan 10, 2019 in Blockchain by Omkar
• 69,220 points
2,490 views
0 votes
1 answer

Python request module for bitcoin json rpc

This should work: #!/usr/bin/env python import getpass import json import requests ...READ MORE

answered Aug 28, 2018 in Blockchain by digger
• 26,740 points
2,383 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
1,942 views
+1 vote
3 answers

Removing double quotes from a string from JSON response in PHP

Just remove the json_encode call, and it should work: $resp ...READ MORE

answered Sep 12, 2018 in Blockchain by digger
• 26,740 points
45,295 views
+1 vote
1 answer
0 votes
1 answer

How to get all address and send ethers in solidity using a loop?

I found a similar code somewhere: contract  Holders{ uint ...READ MORE

answered Jul 31, 2018 in Blockchain by digger
• 26,740 points
2,768 views
0 votes
1 answer

How to call function in one contract from another contract in private blockchain?

Please check whether you have byzantiumBlock: 0 in your ...READ MORE

answered Oct 1, 2018 in Blockchain by digger
• 26,740 points
873 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP