Python subprocess with pathlib on Windows bug

Related: Idiomatic pathlib Python


Python 3.6 made pathlib part of Python standard library. On Windows only, subprocess seems to have a TypeError bug with pathlib.Path input.

Windows bug

Python script with problem

from pathlib import Path
import subprocess

P = Path('~').expanduser()

O = subprocess.check_output(['dir',P])

print(O)

which should list the user’s home directory on Windows, but instead yields on Windows only:

TypeError: argument of type ‘WindowsPath’ is not iterable.

Workaround

convert the Path into a string (this is annoying, and shouldn’t be necessary in Python ≥ 3.6)

O = subprocess.check_output(['dir',str(P)])

Categories:

Updated:

Leave a Comment