Search code examples
pythonfileinteger

Why does file.read() return '' in python


I am making a blockchain, I am storing the latest block in a file named lb.store ,but my code to open and read the file returns ''.

Here is the Error.

Traceback (most recent call last):
  File "C:\Users\ShayanNew\Documents\programming\Python\Blockchain\Node\node.py", line 48, in <module>
    recieve_request()
  File "C:\Users\ShayanNew\Documents\programming\Python\Blockchain\Node\node.py", line 39, in recieve_request
    add_block(data)
  File "C:\Users\ShayanNew\Documents\programming\Python\Blockchain\Node\node.py", line 7, in add_block
    new_block_number = int(lblock_number) + 1
ValueError: invalid literal for int() with base 10: ''

Here is the full code that caused this:

lblock_numberf = open("lb.store","a+")
    lblock_number = lblock_numberf.read()
    lblock_numberf.close()
    new_block_number = int(lblock_number) + 1

Solution

  • It looks like that the file you're trying to read is either empty or the end of the string appears to be <"">. Play around the values to debug the problem