乌鸦乌鸦你的小虎牙 2023-11-07 10:38 采纳率: 100%
浏览 14
已结题

python3 os.system() 突然不能调用dos命令

python3 os.system() 突然不能调用dos命令

![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/214436423996155.png "#le


```python
C:\Users\J01058253>python
Python 3.12.0 (tags/v3.12.0:0fb18b0, Oct  2 2023, 13:03:39) [MSC v.1935 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import subprocess
>>> subprocess.call('cmd /c dir', shell=True)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Program Files\Python312\Lib\subprocess.py", line 389, in call
    with Popen(*popenargs, **kwargs) as p:
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Python312\Lib\subprocess.py", line 1026, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "C:\Program Files\Python312\Lib\subprocess.py", line 1538, in _execute_child
    hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
FileNotFoundError: [WinError 2] 系统找不到指定的文件。
>>> import os
>>> os.system('dir')
-1
>>> os.system(r'C:\Windows\System32\calc.exe')
-1
>>>

```

  • 写回答

2条回答 默认 最新

  • 关注

    python安装路径为program files 已卸载重装,排除了programfiles 路径空格的影响,使用管理员权限运行脚本,返回值仍旧为-1
    解决分割线-----------------------------------
    检查windows与os.system相关的环境变量ComSpec是否发生变化,应该为 C:\WINDOWS\system32\cmd.exe

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 10月23日
  • 已采纳回答 10月15日
  • 创建了问题 11月7日