您将要使用subprocess.Popen:
>>> import subprocess
>>> r = subprocess.Popen(['ls', '-l']) #List files on a linux system. Equivalent of dir on windows.
>>> output, errs = r.communicate()
>>> print(output)
Total 72
# My file list here
Run Code Online (Sandbox Code Playgroud)
该Popen-construtor接受的参数作为第一个参数列表。该列表以命令开头(在本例中为ls),其余值为该命令的开关和其他参数。上面的示例ls -l在终端(或命令行或控制台)上编写为。Windows等效
>>> r = subprocess.Popen(['dir', '/A'])
Run Code Online (Sandbox Code Playgroud)