首页 > 解决方案 > subprocess.Popen [Errno 2] 没有这样的文件或目录

问题描述

我编写了一些在 Stream 中使用的脚本。我做了一些,现在我尝试做一个集中​​我所有脚本的启动器。

我认为我在子流程方面做得很好,但我在 atm 遇到了一些困难。

我尝试使用这一行启动脚本:

subprocess.Popen(['python', "E:/path/rocksmith/rocksmith.pyw"], shell=True)

但是 python 给我一个 No such file or directory 错误。在我想启动的脚本中(rocksmith.pyw),我用 open() 打开了一些文本文件,我猜 python 找不到 .txt 文件,因为 .txt 不在启动器目录中,对吗?

我的目录是这样的:

Launcher folder
 - launcher.py
   * Script 1 folder
     - Script1.py
     - text.txt
   * Script 2 folder
     - Script2.py
     - text.txt
   * Script 3 folder
     - Script3.py
     - image.gif

我不知道我的解释是否可以。我只是想在“超级脚本”中启动一些脚本。

谢谢。

标签: pythonsubprocess

解决方案


rocksmith.pyw如果在尝试打开文件时发生错误,您可能需要使用cwdkwarg 来Popen. 这将更改运行脚本的工作目录,因此其中的相对路径rocksmith.pyw将按照您的预期解析。

subprocess.Popen(['python', "E:/path/rocksmith/rocksmith.pyw"], cwd='/path/to/rocksmith/stuff')

推荐阅读