简体   繁体   English

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

[英]python as a “batch” script (i.e. run commands from python)

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? 我如何从python运行命令,使程序运行时,将替换脚本? The program is interactive (for instance, unison) and keeps printing lines and asking for user input all the time. 该程序是交互式的(例如,同步)并保持打印行并始终要求用户输入。

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); 我试过os.execl,但它一直告诉我“无效的参数”,而且,它没有找到程序名称(不搜索PATH变量); I have to give it the full 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 谢谢Claudiu,这几乎是我想要的,除了一点点:我希望函数在程序退出时结束,但是当我一致地尝试它时,它不会将控制返回到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? 怎么绕过这个?

You should create a new processess using the subprocess module . 您应该使用进程模块创建一个新进程。

I'm not fluent in windows processes but its Popen function is cross-platform, and should be preffered to OS specific solutions. 我不熟悉windows进程,但它的Popen功能是跨平台的,应该优先考虑OS特定的解决方案。

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). 编辑:我认为你应该更喜欢Subprocess模块​​到os。* OS特定的功能,它是跨平台和更pythonic(只是google它)。 You can wait for the result easily, and cleanly : 您可以轻松, 干净地等待结果:

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

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 运行该命令就像从批处理文件中运行一样

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! 如果您想要更详细的答案,请使用更多信息编辑您的问题!

os.execlp should work. os.execlp应该工作。 This will search your path for the command. 这将搜索您的路径以获取该命令。 Don't give it any args if they're not necessary: 如果没有必要,不要给它任何args:

>>> 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>

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

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