python 作为“批处理”脚本(即从 python 运行命令)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/311601/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 19:53:12  来源:igfitidea点击:

python as a "batch" script (i.e. run commands from python)

pythonscriptingbatch-file

提问by hasen

I'm working in a windows environment (my laptop!) and I need a couple of scripts that run other programs, pretty much like a windows batch file.

我在 Windows 环境(我的笔记本电脑!)中工作,我需要几个运行其他程序的脚本,非常像 Windows 批处理文件。

how can I run a command from python such that the program when run, will replace the script? The program is interactive (for instance, unison) and keeps printing lines and asking for user input all the time.

如何从 python 运行命令,以便程序在运行时替换脚本?该程序是交互式的(例如,一致),并始终保持打印线并要求用户输入。

So, just running a program and printing the output won't suffice. The program has to takeover the script's input/output, pretty mcuh like running the command from a .bat file.

所以,仅仅运行一个程序并打印输出是不够的。程序必须接管脚本的输入/输出,就像从 .bat 文件运行命令一样。

I tried os.execl but it keeps telling me "invalid arguments", also, it doesn't find the program name (doesn't search the PATH variable); I have to give it the full path ..?!

我试过 os.execl 但它一直告诉我“无效参数”,而且,它没有找到程序名称(不搜索 PATH 变量);我必须给它完整的路径..?!

basically, in a batch script I can write: unison profile

基本上,在我可以编写的批处理脚本中:unison profile

how can I achieve the same effect in python?

我怎样才能在python中达到同样的效果?

EDIT:

编辑:

I found out it can be done with os.system( ... )and since I cannot accept my own answer, I'm closing the question.

我发现它可以完成,os.system( ... )因为我不能接受我自己的答案,所以我结束了这个问题。



EDIT: this was supposed to be a comment, but when I posted it I didn't have much points.

编辑:这应该是一个评论,但是当我发布它时,我没有太多的观点。

Thanks Claudiu, that's pretty much what I want, except for a little thing: I want the function to end when the program exits, but when I try it on unison, it doesn't return control to the python script, but to the windows command line environment

谢谢克劳迪乌,这几乎就是我想要的,除了一件小事:我希望函数在程序退出时结束,但是当我一致尝试时,它不会将控制权返回给 python 脚本,而是返回给 Windows命令行环境

>>> os.execlp("unison")

C:\>Usage: unison [options]
    or unison root1 root2 [options]
    or unison profilename [options]

For a list of options, type "unison -help".
For a tutorial on basic usage, type "unison -doc tutorial".
For other documentation, type "unison -doc topics".

C:\>
C:\>
C:\>

how to get around this?

如何解决这个问题?

采纳答案by hasen

I found out that os.system does what I want,

我发现 os.system 做我想做的事,

Thanks for all that tried to help.

感谢所有试图提供帮助的人。

os.system("dir")

runs the command just as if it was run from a batch file

运行命令就像从批处理文件中运行一样

回答by Piotr Lesnicki

You should create a new processess using the subprocess module.

您应该使用subprocess 模块创建一个新流程。

I'm not fluent in windows processes but its Popen function is cross-platform, and should be preffered to OS specific solutions.

我不熟悉 Windows 进程,但它的 Popen 功能是跨平台的,应该优先于操作系统特定的解决方案。

EDIT: I maintain that you should prefer the Subprocess module to os.* OS specific functions, it is cross-platform and more pythonic (just google it). You can wait for the result easily, and cleanly:

编辑:我认为你应该更喜欢 Subprocess 模块而不是 os.* 操作系统特定的功能,它是跨平台的,而且更像 pythonic(只是谷歌它)。您可以轻松,干净地等待结果:

import os
import subprocess
unison = os.path.join(os.path.curdir, "unison")
p = subprocess.Popen(unison)
p.wait()

回答by orestis

import subprocess

proc = subprocess.Popen(['unison', 'profile'], stderr=subprocess.PIPE,      
                        stdout=subprocess.PIPE, stdin=subprocess.PIPE)

proc.stdin.write('user input')
print proc.stdout.read()

This should help you get started. Please edit your question with more information if you want a more detailed answer!

这应该可以帮助您入门。如果您想要更详细的答案,请使用更多信息编辑您的问题!

回答by Claudiu

os.execlpshould work. This will search your path for the command. Don't give it any args if they're not necessary:

os.execlp应该管用。这将在您的路径中搜索命令。如果没有必要,不要给它任何参数:

>>> import os
>>> os.execlp("cmd")

D:\Documents and Settings\Claudiu>Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

D:\Documents and Settings\Claudiu>