title: Python - Increment file by 1
body: <p>I have a file with the following hex values:</p>
  
  <pre><code>00 00 00 00 00 00 00 00
  </code></pre>
  
  <p>What I want to do is essentially increment this file by one. i.e. after running the program, I will be left with:</p>
  
  <pre><code>00 00 00 00 00 00 00 01
  </code></pre>
  
  <p>And when I get to:</p>
  
  <pre><code>00 00 00 00 00 00 00 FF
  </code></pre>
  
  <p>I want the next increment to leave me with:</p>
  
  <pre><code>00 00 00 00 00 00 01 00
  </code></pre>
  
  <p>Is there a simple way to do this with a byte array, or something similar?</p>
  
  <p>EDIT:</p>
  
  <p>This is something like my previous code:</p>
  
  <pre><code>filename  = 'file'
  
  with open(filename, "rb+") as file:
      seek = -1
      while True:
          file.seek(seek, 2)
          value = file.read(1).encode('hex')
          file.seek(seek, 2)
          if value != 0xFF:
              file.write(str(int(value, 16) + 0x1))
              break
          else:
              file.write(str(0x00))
              seek -= 1
  </code></pre>
  
  <p>It gives... unexpected results.</p>