使用 python 在不同文件夹中执行 npm 脚本

问题描述 投票:0回答:1

有人可以告诉我如何使用 Python 脚本在任何文件夹中执行“npm run start”。但请使用“os”运算符而不是“subprocess”。

编辑: 我需要一个 python 脚本,该脚本转到特定文件夹然后执行 “npm 运行开始”。我怎样才能做到这一点?

python command-line subprocess os.system
1个回答
3
投票

您可以“更改目录”以在所选文件夹中运行代码

os.chdir("path/to/folder")
os.system("npm run start") 

或者您可以使用

;
&&
os.system

中执行此操作
os.system("cd path/to/folder ; npm run start") 

os.system("cd path/to/folder && npm run start") 

或使用

cwd
中的
subprocess
设置当前工作目录

subprocess.run("npm run start", shell=True, cwd="path/to/folder")

subprocess.run(["npm", "run", "start"], cwd="path/to/folder")

subprocess

中的其他方法类似
© www.soinside.com 2019 - 2024. All rights reserved.