使用Python运行Windows命令 [英] Running Windows commands with Python

查看:288
本文介绍了使用Python运行Windows命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

搜索这个论坛我发现了一个从Python运行Unix命令的例子;我想知道是否有办法使用Python在字符串上获取windows命令的输出。

Searching this forum I found an example of running Unix commands from Python;I wonder if there is a way to get the output of windows commands on a string using Python.


我相信只有* nix环境才能将命令行功能内置到Python中。是否有Windows解决方案?
I believe command line functionality is built in into Python only for *nix environment.Is there''s a windows solution?



提前致谢;

Miago。

Thanks in advance;
Miago.

推荐答案


搜索这个论坛我找到了一个从Python运行Unix命令的例子;我想知道是否有办法使用Python.Thanks提前在字符串上获取windows命令的输出;

Miago。
Searching this forum I found an example of running Unix commands from Python;I wonder if there is a way to get the output of windows commands on a string using Python.Thanks in advance;
Miago.



这取决于您尝试连接的Window应用程序。大多数Microsoft产品和某些第三方产品都是COM(通用对象模型)。使用PythonWin包,您可以像使用VB脚本一样向这些应用程序发送命令。如果您的python程序有一个完整的GU接口,您可以选择所需的文本,然后使用VB命令复制,然后获取程序中的剪贴板内容。或者您可以保存文本并读取生成的文件。我见过一个从MS Word复制文本并将其粘贴到网页的示例。可能性是无穷无尽的,但我对PythonWin并不熟悉。不过,这个论坛上有一些人。我使用wxPython,所以我的所有Python程序几乎可以完全访问Windows的功能,如剪贴板。 PythonWin是另一种方法,但你必须首先了解Windows API。

希望这对你有用,

Barton

It sort of depends on which Window app you are trying to interface to. Most Microsoft products and some third party products are COM (Common Object Model) aware. With the PythonWin package, you can send commands to these apps as if you were using a VB script. If your python program has a full GU interface you could Select the text that you want, then Copy using the VB commands then get the clipboard contents in your program. Or you might be able to SaveAs text and read the resulting file. I''ve seen an example that copied text from MS Word and pasted it to a web page. The possibilities are endless, but I''m not very familiar with PythonWin. There are some on this forum who are, though. I use wxPython so all my Python programs have almost complete access to Windows'' features like the clipboard. PythonWin is another way to do this, but you pretty much have to know the Windows API first.
Hope this is useful to you,
Barton



搜索这个论坛我找到了一个从Python运行Unix命令的例子;我想知道是否有办法使用Python在字符串上输出windows命令。提前谢谢;

Miago。
Searching this forum I found an example of running Unix commands from Python;I wonder if there is a way to get the output of windows commands on a string using Python.Thanks in advance;
Miago.



您可以通过重新分配sys.stdout来执行此操作。

You may be able to do this by reassigning sys.stdout.



搜索这个论坛我发现了一个从Python运行Unix命令的例子;我想知道是否有办法使用Python在字符串上获取windows命令的输出。提前谢谢;

Miago。
Searching this forum I found an example of running Unix commands from Python;I wonder if there is a way to get the output of windows commands on a string using Python.Thanks in advance;
Miago.



好​​点,motoma。我在想Windows应用程序,但可能有一种方法可以将用于回显文本的Windows命令的输出重定向到命令行。有人有这方面的经验吗?

Good point, motoma. I was thinking of Windows apps, but there may be a way to redirect the output of a Windows command that is designed to echo text to the command-line. Anybody have any experience with this?


这篇关于使用Python运行Windows命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆