title: Decompressing a .bz2 file in Python
body: <p>So, this is a seemingly simple question, but I'm apparently very very dull. I have a little script that downloads all the .bz2 files from a webpage, but for some reason the decompressing of that file is giving me a MAJOR headache.</p>
  
  <p>I'm quite a Python newbie, so the answer is probably quite obvious, please help me.</p>
  
  <p>In this bit of the script, I already have the file, and I just want to read it out to a variable, then decompress that? Is that right? I've tried all sorts of way to do this, I usually get "ValueError: couldn't find end of stream" error on the last line in this snippet. I've tried to open up the zipfile and write it out to a string in a zillion different ways. This is the latest.</p>
  
  <pre><code>openZip = open(zipFile, "r")
  s = ''
  while True:
      newLine = openZip.readline()
      if(len(newLine)==0):
         break
      s+=newLine
      print s                   
      uncompressedData = bz2.decompress(s)
  </code></pre>
  
  <p>Hi Alex, I should've listed all the other methods I've tried, as I've tried the read() way.</p>
  
  <p>METHOD A:</p>
  
  <pre><code>print 'decompressing ' + filename
  
  fileHandle = open(zipFile)
  uncompressedData = ''
  
  while True:            
      s = fileHandle.read(1024)
      if not s:
          break
          print('RAW "%s"', s)
          uncompressedData += bz2.decompress(s)
  
          uncompressedData += bz2.flush()
  
          newFile = open(steamTF2mapdir + filename.split(".bz2")[0],"w")
          newFile.write(uncompressedData)
          newFile.close()   
  </code></pre>
  
  <p>I get the error:  </p>
  
  <pre><code>uncompressedData += bz2.decompress(s)
  ValueError: couldn't find end of stream
  </code></pre>
  
  <p>METHOD B</p>
  
  <pre><code>zipFile = steamTF2mapdir + filename
  print 'decompressing ' + filename
  fileHandle = open(zipFile)
  
  s = fileHandle.read()
  uncompressedData = bz2.decompress(s)
  </code></pre>
  
  <p>Same error :</p>
  
  <pre><code>uncompressedData = bz2.decompress(s)
  ValueError: couldn't find end of stream
  </code></pre>
  
  <p>Thanks so much for you prompt reply. I'm really banging my head against the wall, feeling inordinately thick for not being able to decompress a simple .bz2 file. </p>
  
  <p>By the by, used 7zip to decompress it manually, to make sure the file isn't wonky or anything, and it decompresses fine.</p>