Why do I get a FileNotFoundError when using subprocess?

Viewed 3256

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.

1 Answers

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.

Related