0

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.

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 is placed in a random desktop folder. 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.

From my reaserch on the topic i have to use subprocess, bit im kind of lost on how to use it.

pythonore
  • 37
  • 1
  • 8
  • 3
    Possible duplicate of [Calling an external command in Python](http://stackoverflow.com/questions/89228/calling-an-external-command-in-python) – Taku Feb 27 '17 at 15:52
  • "From my reaserch on the topic i have to use subprocess" Yep, `subprocess` is the correct approach here. I suggest `call` or `check_output`, depending on your specific needs. Try playing around with those for a bit. – Kevin Feb 27 '17 at 15:52

3 Answers3

0

Have you looked into using os.system to execute commands? 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.

os.system(command)

Execute the command (a string) in a subshell. This is implemented by calling the Standard C function system(), and has the same limitations. Changes to sys.stdin, etc. are not reflected in the environment of the executed command. 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(). 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.

On Windows, the return value is that returned by the system shell after running command. The shell is given by the Windows environment variable COMSPEC: it is usually cmd.exe, which returns the exit status of the command run; on systems using a non-native shell, consult your shell documentation.

The subprocess module provides more powerful facilities for spawning new processes and retrieving their results; 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.

Noctis Skytower
  • 21,433
  • 16
  • 79
  • 117
  • os.system is deprecated... use subprocess instead – Corey Goldberg Feb 27 '17 at 16:22
  • As of the time of this writing, `os.system` is not listed as being deprecated according to the [official documentation](https://docs.python.org/3/library/os.html#os.system) for Python 3.6. – Noctis Skytower Feb 27 '17 at 16:34
  • perhaps not officially deprecated... but it's not the preferred way. from the `os.system` docs: "The subprocess module provides more powerful facilities for spawning new processes and retrieving their results; using that module is preferable to using this function" – Corey Goldberg Feb 27 '17 at 16:46
0

use the subprocess module.


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

you can still use subprocess.call, but for Python versions >3.4, it's preferred to use the newer 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 (i.e. a list).

Corey Goldberg
  • 59,062
  • 28
  • 129
  • 143
-1

Following is the snippet which I keep it handy always to run windows commands.

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
  • [Please don't post identical answers to multiple questions](https://meta.stackoverflow.com/a/256849/354577). – ChrisGPT was on strike May 08 '20 at 14:03
  • I am new to StackOverflow and I did not know that we should not answer so. Should I redirect them to the original question? – srinivas-beerge May 08 '20 at 14:05
  • From the link I provided: "If you can use the identical answer across multiple posts, then that is a strong indicator that you have found duplicate questions. Instead of posting the same answer you should vote to close these questions. On the other hand, if you believe these are not duplicate questions then you should not be posting the exact same answer all of them and you should alter the answer to be specific to each problem asked." Voting to close requires 3000+ reputation, so you won't be able to do that yet. Please provide _new_ questions and answers to earn reputation. – ChrisGPT was on strike May 08 '20 at 14:13
  • Or, as the second option says, add an answer specifically tailored for each question. – ChrisGPT was on strike May 08 '20 at 14:14
  • 1
    Thank you for you time and answer. I will follow it. – srinivas-beerge May 08 '20 at 14:22