python中调用os.system("ls")报错无法识别ls指令,是什么原因呢?

执行
import os
os.system(“ls”)

窗口显示结果
‘ls’ is not recognized as an internal or external command,
operable program or batch file.

讨论数量: 5
Jason990420

你是不是在 Window 系统中调用 Linux 的 ls 命令 ? 如果是这样你应该要使用 dir

import os
os.system(dir)
2年前 评论
Gboy (楼主) 2年前
Gboy (楼主) 2年前
Jason990420

os.system(command)

Execute the command (a string) in a subshell. The shell is given by the Windows environment variable COMSPEC: it is usually cmd.exe, which returns the exit status of the command run; on systems using a non-native shell, consult your shell documentation.

如果你想在 Windows 下使用 ls, 那就在你的路径下, 建立一个 ls.exe, ls.comls.bat 就可以了.

比如 ls.bat,

@echo off
dir
2年前 评论
Gboy (楼主) 2年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!