2

My code:

parser = xml.sax.make_parser()
handler = WikiXmlHandler()
parser.setContentHandler(handler)
for line in subprocess.Popen(['bzcat'], 
                            stdin=open(path), 
                            stdout=subprocess.PIPE).stdout:
    try:
        parser.feed(line)
    except StopIteration:
        break

The error:

File "c:/Users/Leon/Documents/VS Code/film.py", line 92, in <module>
    for line in subprocess.Popen(['bzcat'],
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.1520.0_x64__qbz5n2kfra8p0\lib\subprocess.py", line 854, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.1520.0_x64__qbz5n2kfra8p0\lib\subprocess.py", line 1307, in _execute_child
    hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] Das System kann die angegebene Datei nicht finden

I do import subprocess.

The file C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.1520.0_x64__qbz5n2kfra8p0\lib\subprocess.py exists.

AcK
  • 2,063
  • 2
  • 20
  • 27
Leon
  • 23
  • 1
  • 5

1 Answers1

4

Apparently bzcat is not in your PATH. It's not complaining that it can't find subprocess.py, it's complaining that it can't find the command you wanted to run.

Anyway, you don't need a subprocess to read .bz2 files; see the bz2 module in the Python standard library.

import bz2

with bz2.open(path, 'rt') as handle:
    for line in handle:
        parser.feed(line)

Like basically any compressed format, bzip2 is a binary format, so you should have used open(path, 'rb'). The bz2.open() function (slightly curiously) defaults to binary mode; if you want to read and decode as text, you have to specify 'rt' explicitly.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • I just tried to recreate this on Windows and you're right, that does seem to be the case. Do you think this might be a Windows only bug? – Pratham_Amitabh Aug 09 '20 at 10:36
  • What bug? If they don't have `bzcat` installed that's only typical on Windows, but you'd get the same symptom on e.g. Alpine Linux if you had forgotten to install `bzcat`, or if you messed up your `PATH` so it can't be found. – tripleee Aug 09 '20 at 10:46
  • 1
    What is a Windows-only bug is that you *can* pass in a string with spaces or even a list with a string with spaces with `shell=False`; but the Windows people seem to think this is a feature, not a bug. – tripleee Aug 09 '20 at 10:48