title: How to programmatically count the number of files in an archive using python
body: <p>In the program I maintain it is done as in:</p>
  
  <pre><code># count the files in the archive
  length = 0
  command = ur'"%s" l -slt "%s"' % (u'path/to/7z.exe', srcFile)
  ins, err = Popen(command, stdout=PIPE, stdin=PIPE,
                   startupinfo=startupinfo).communicate()
  ins = StringIO.StringIO(ins)
  for line in ins: length += 1
  ins.close()
  </code></pre>
  
  <ol>
  <li>Is it really the only way ? I can't seem to find <a href="http://sevenzip.osdn.jp/chm/cmdline/commands/" rel="noreferrer">any other command</a> but it seems a bit odd that I can't just ask for the number of files</li>
  <li><p>What about error checking ? Would it be enough to modify this to:</p>
  
  <pre><code>proc = Popen(command, stdout=PIPE, stdin=PIPE,
               startupinfo=startupinfo)
  out = proc.stdout
  # ... count
  returncode = proc.wait()
  if returncode:
      raise Exception(u'Failed reading number of files from ' + srcFile)
  </code></pre>
  
  <p>or should I actually parse the output of Popen ?</p></li>
  </ol>
  
  <p>EDIT: interested in 7z, rar, zip archives (that are supported by 7z.exe) - but 7z and zip would be enough for starters</p>