0

I primary work on GNU/Linux but sometimes have to deal with Windows.

I found out that subprocess isn't able to see the home folder on Windows when shell=False (default).

#!/usr/bin/env python3
from pathlib import Path
import subprocess

homepath = Path.home()

try:
    subprocess.Popen(['dir', homepath])  # WinError 2
except FileNotFoundError:
    print('Can not find')

subprocess.Popen(['dir', homepath], shell=True)  # OK

I wonder why it is that way. Because on GNU/Linux there is no such a restriction and I'm able to run subprocess activies with HOME involved with shell=True. But using shell=True

buhtz
  • 10,774
  • 18
  • 76
  • 149
  • 1
    `dir` is a `cmd` built-in; it's `dir` it can't find, not your directory. `subprocess` works slightly differently on Windows but this is not an example of that (other than in that it accepts a list of tokens with `shell=True`, which other platforms don't directly support). – tripleee Jan 31 '23 at 17:21

0 Answers0