首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python子进程在Windows 7上不工作

Python子进程在Windows 7上不工作
EN

Stack Overflow用户
提问于 2014-09-11 18:47:59
回答 1查看 3K关注 0票数 0

有人能解释一下这里发生了什么吗?即使是内置的cmd.exe命令也无法工作:

代码语言:javascript
复制
Python 3.4.0 (v3.4.0:04f714765c13, Mar 16 2014, 19:25:23) [MSC v.1600 64 bit (AM
D64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import subprocess
>>> subprocess.call('dir')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "D:\Python34\lib\subprocess.py", line 535, in call
    with Popen(*popenargs, **kwargs) as p:
  File "D:\Python34\lib\subprocess.py", line 848, in __init__
    restore_signals, start_new_session)
  File "D:\Python34\lib\subprocess.py", line 1104, in _execute_child
    startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
>>>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-09-11 18:49:27

要使用'dir',必须传递shell=True

代码语言:javascript
复制
>>> import subprocess
>>> subprocess.call('dir', shell=True)

您必须这样做,因为dir是内置在shell中的,它不是一个独立的控制台应用程序。在subprocess.Popen 文档中也提到了这一点。

在使用shell=True的Windows上,COMSPEC环境变量指定默认的shell。在Windows上唯一需要指定shell=True的时候,要执行的命令被内置到(例如dir或)中。您不需要shell=True来运行批处理文件或基于控制台的可执行文件。

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25794941

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档