You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用subprocess.Popen执行命令报错:系统找不到指定文件

解决subprocess.Popen触发的FileNotFoundError问题

错误根源

你遇到的FileNotFoundError: [WinError 2]主要由两种情况导致:

  • 命令格式不匹配:reliable_recv()返回的command是列表类型,但开启shell=True时,subprocess.Popen要求传入字符串形式的完整命令行。若传入列表,它会把列表第一个元素当作可执行文件路径,若该文件不在当前目录或系统PATH中,就会触发找不到文件的错误。
  • 可执行文件路径缺失:即使命令是字符串,若命令中的可执行文件没有完整路径,且不在系统PATH环境变量中,Windows无法定位到该文件。

修复方案

1. 对齐命令格式

先确认reliable_recv()返回的command类型(可在shell()函数中添加print(type(command), command)查看):

  • 若为列表类型:
    方法一:将列表拼接为字符串后传入:

    execute = subprocess.Popen(' '.join(command), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
    

    方法二:关闭shell=True(更安全,推荐):

    execute = subprocess.Popen(command, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
    

    注意:关闭shell=True时,需确保命令中的可执行文件能被系统找到,或直接使用完整路径。

  • 若为字符串类型:
    确保命令中的可执行文件在系统PATH中,或使用完整路径,示例:

    # 用完整路径执行cmd命令
    command = "C:\\Windows\\System32\\cmd.exe /c dir"
    

2. 额外排查项

  • 检查客户端传递的命令是否为空或格式错误。
  • 确认backdoor.py运行环境的PATH变量包含所需可执行文件的路径。

内容的提问来源于stack exchange,提问作者Kfiatek430

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 07:00:58