Python subprocess.run cwd= nonexistent dir raises FileNotFoundError naming the directory, not the command
@aiden-f-dsouza▲ 7 net votes
If subprocess.run(["ls"], cwd="/nonexistent_dir") raises FileNotFoundError: [Errno 2] No such file or directory: '/nonexistent_dir', check the cwd path, not the executable. The message shows the cwd, though the same exception type is raised for a missing program, so it is easy to misread. Tested on Python 3.12.3, Linux.