简体   繁体   English

如何从 python 运行 cmd.exe 命令?

[英]How to run cmd.exe commands from python?

I would like to run to run a script that opens GUI in which i press a start button which runs (opens, writes, runs) cmd.exe command line.我想运行一个脚本来打开 GUI,我在其中按下运行(打开、写入、运行)cmd.exe 命令行的开始按钮。

from tkinter import* 
import sys, string, os
import subprocess
class App:
  def __init__(self, master):
    frame = Frame(master)
    frame.pack()

    self.slogan = Button(frame,text="Start",command=self.start)
    self.slogan.pack(side=LEFT)


  def start(self):
    subprocces.call([])

root = Tk()
app = App(root)
root.mainloop()

Command is following : "ConverterApp.exe" file1.x file1.y命令如下:“ConverterApp.exe”file1.x file1.y

ConverterApp is placed in a random desktop folder. ConverterApp 被放置在一个随机的桌面文件夹中。 What is does it converts one type of photo to another.它将一种类型的照片转换为另一种类型的照片是什么。 And right now i have to use the command above for every photo, so i want to write pyhton program which will convert all .x files in folder to .y.现在我必须对每张照片使用上面的命令,所以我想编写 pyhton 程序,它将文件夹中的所有 .x 文件转换为 .y。

From my reaserch on the topic i have to use subprocess , bit im kind of lost on how to use it.从我对这个主题的研究来看,我必须使用subprocess ,我对如何使用它有点迷茫。

Have you looked into using os.system to execute commands?您是否考虑过使用os.system来执行命令? It is simple to use and may be sufficient to accomplish what you are trying to do.它使用简单,可能足以完成您想要做的事情。 On Windows, it usually runs cmd.exe for you.在 Windows 上,它通常会为您运行cmd.exe

os.操作系统system ( command )系统命令

Execute the command (a string) in a subshell.在子shell中执行命令(一个字符串)。 This is implemented by calling the Standard C function system(), and has the same limitations.这是通过调用标准 C 函数 system() 实现的,并且具有相同的限制。 Changes to sys.stdin, etc. are not reflected in the environment of the executed command.对 sys.stdin 等的更改不会反映在执行命令的环境中。 If command generates any output, it will be sent to the interpreter standard output stream.如果命令生成任何输出,它将被发送到解释器标准输出流。

On Unix, the return value is the exit status of the process encoded in the format specified for wait().在 Unix 上,返回值是以为 wait() 指定的格式编码的进程的退出状态。 Note that POSIX does not specify the meaning of the return value of the C system() function, so the return value of the Python function is system-dependent.注意POSIX没有指定C system()函数返回值的含义,所以Python函数的返回值是系统相关的。

On Windows, the return value is that returned by the system shell after running command.在 Windows 上,返回值是系统 shell 在运行命令后返回的值。 The shell is given by the Windows environment variable COMSPEC: it is usually cmd.exe, which returns the exit status of the command run; shell由Windows环境变量COMSPEC给出:通常是cmd.exe,返回命令运行的退出状态; on systems using a non-native shell, consult your shell documentation.在使用非本地 shell 的系统上,请查阅您的 shell 文档。

The subprocess module provides more powerful facilities for spawning new processes and retrieving their results; subprocess 模块提供了更强大的工具来生成新进程并检索它们的结果; using that module is preferable to using this function.使用该模块比使用此功能更可取。 See the Replacing Older Functions with the subprocess Module section in the subprocess documentation for some helpful recipes.有关一些有用的秘诀,请参阅子流程文档中的用子流程模块替换旧函数部分。

Availability: Unix, Windows.可用性:Unix、Windows。

use the subprocess module.使用subprocess模块。


Python 2: https://docs.python.org/2/library/subprocess.html Python 2: https : //docs.python.org/2/library/subprocess.html

example:例子:

import subprocess
subprocess.call('ConverterApp.exe', 'file1.x', 'file1.y'])

Python 3: https://docs.python.org/3/library/subprocess.html Python 3: https : //docs.python.org/3/library/subprocess.html

you can still use subprocess.call , but for Python versions >3.4, it's preferred to use the newer subprocess.run .您仍然可以使用subprocess.call ,但对于 Python 版本 >3.4,最好使用较新的subprocess.run

example:例子:

import subprocess
subprocess.run(['ConverterApp.exe', 'file1.x', 'file1.y'])

Note: in both subprocess.call , and subprocess.run , your command should be passed as a sequence (ie a list).注意:在subprocess.callsubprocess.run ,您的命令应该作为序列(即列表)传递。

Following is the snippet which I keep it handy always to run windows commands.以下是我始终随身携带以运行 Windows 命令的片段。

import subprocess
result = []
win_cmd = 'ipconfig'
process = subprocess.Popen(win_cmd,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE )
for line in process.stdout:
    print line
result.append(line)
errcode = process.returncode
for line in result:
    print line

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

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