在Python中隐藏CMD窗口运行子进程,你可以使用`subprocess`模块。以下是一些方法:
使用`subprocess`模块
import subprocess
cmd = 'dir' 你想要执行的命令
output = subprocess.check_output(cmd, shell=True, stderr=subprocess.STDOUT)
print(output.decode('utf-8'))
使用`pythonw.exe`
在Windows操作系统下,使用`pythonw.exe`代替`python.exe`来运行Python脚本可以隐藏CMD窗口。
pythonw script.py
使用`pyinstaller`打包
`pyinstaller`可以将Python脚本打包成独立的可执行文件,从而隐藏CMD窗口。
pip install pyinstaller
pyinstaller --onefile --icon=img.ico main.py -w
以上方法可以帮助你在Python中隐藏CMD窗口运行子进程。请根据你的需求选择合适的方法