简体   繁体   English

从python脚本执行cmd.exe命令

[英]Executing a cmd.exe command from a python script

I have a python script callee.py which is in a certain directory and that directory is part of the Windows PATH environment variable. 我在某个目录中有一个python脚本callee.py ,该目录是Windows PATH环境变量的一部分。 In the Windows command prompt cmd.exe I can simply execute it as 在Windows命令提示符cmd.exe我可以简单地执行为

> callee.py arg1 arg2

Now, I have another python script caller.py in another directory. 现在,我在另一个目录中有另一个python脚本caller.py To execute the first script I would like to use the subprocess module like 要执行的第一个脚本,我想用subprocess模块状

subprocess.Popen("callee.py arg1 arg2")

but this does not work, because it does not get that it should actually call python.exe callee.py ... . 但这是行不通的,因为它没有得到应该实际调用python.exe callee.py ... Furthermore, 此外,

subprocess.Popen("python.exe callee.py arg1 arg2")

does not work, because it does not find callee.py . 不起作用,因为它找不到callee.py

Is there something like subprocess.Popen which accepts the very same kind of commands that the Windows command prompt cmd.exe does? 是否有类似subprocess.Popen东西可以接受与Windows命令提示符cmd.exe一样的命令?

Is callee.py executable? callee.py是可执行文件吗? Or, why don't you give the full path? 或者,为什么不给出完整的路径?

subprocess.Popen("python.exe c:\full_path\to\callee.py arg1 arg2")

Too late, don't consider this answer. 为时已晚,不要考虑这个答案。

它像这样工作

subprocess.Popen("cmd.exe /c callee.py arg1 arg2")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM