【发布时间】:2020-07-20 06:36:33
【问题描述】:
import subprocess
subprocess.call('ls', shell=True)
输出:
'ls' is not recognized as an internal or external command, operable program or batch file.
【问题讨论】:
-
ls不是 Windows 上的命令。如果你想在 windows 上使用 linux 命令,请使用 WSL 或 Cygwin。 -
您希望
ls在 Windows 上做什么?它目前的行为与您在cmd.exe中键入它的行为相同?您是否尝试运行 Powershell 别名?还是 Unix 命令? -
出于可移植性和效率的原因,当有提供相同功能的 Python 标准模块时,您应该避免运行外部命令。在这种情况下,您可以致电
os.scandir获取目录列表和文件信息。
标签: python windows subprocess