0x00 环境
名称 | |
---|---|
OS系统 | windows 7 64bit |
编程语言 | python 3.7 |
0x01 问题
使用subprocess.Popen
执行命令报错,错误信息如下:
Traceback (most recent call last):
File "xxx.py", line 4, in addLabel
output = Popen('dir',stdout=PIPE)
File "D:\Program Files\Python3\lib\subprocess.py", line 775, in __init__
restore_signals, start_new_session)
File "D:\Program Files\Python3\lib\subprocess.py", line 1178, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] 系统找不到指定的文件。
源码如下
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from subprocess import Popen, PIPE
output = Popen('dir',stdout=PIPE)
0x02 原因
dir
命令是cmd.exe
或者powershell.exe
才能理解的命令。在Windows环境下subprocess.Popen
默认是不会调用cmd.exe
的。所以需要指定。
subprocess.Popen(["cmd", "/c", "dir", "c:\\Users"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
0x03 参考链接
https://stackoverflow.com/questions/49018413/filenotfounderror-subprocess-popendir-windows-7