Solved

Hiding the command prompt in my program.

Posted on 2008-06-22
9
1,379 Views
Last Modified: 2010-04-21
Hello everyone,

I'm currently developing a tool in Python using wxPython for my GUI.
Now I'm using wx.Shell() to execute commands to the command prompt.
Now this all works fine, but the command prompts keep popping up, is there a way to hide these?

Here's a copy of the modules I imported so far.
import wx
import os, sys
import string
import win32api
import re

Regards,
Olrik
0
Comment
Question by:Artellos
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
  • 2
9 Comments
 
LVL 9

Expert Comment

by:ghostdog74
ID: 21843085
what is it that you need to call a shell command, that Python cannot do? A third party application ?
0
 
LVL 3

Author Comment

by:Artellos
ID: 21843743
I'm probably doing it in a really inefficient way (feel free to tip about that.) but I'm using wx.Shell() from wxPython to call ipconfig / dxdiag and even notepad for example.

Regards,
Olrik
0
 
LVL 9

Expert Comment

by:ghostdog74
ID: 21843849
what are you trying to get out of ipconfig ? the IP address? for cross platform scripts/application its best to use what Python already provides. eg

import socket
socket.gethostbyname(socket.gethostname())

since you are using wxPython, why not create a simple text editor yourself, instead of calling Notepad, which other platforms like Linux doesn't have.
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 3

Author Comment

by:Artellos
ID: 21844054
I'm making a tool that will only be used for Windows, Cross platform is not important to me.
I want to view everything an ipconfig /all shows, but ipconfig is not the only one.
Also "DxDiag /t <outputfile>" is run using a wx.Shell() in my program.

But I suppose there's no way to hide the command prompt?

Regards,
Olrik
0
 
LVL 29

Accepted Solution

by:
pepr earned 500 total points
ID: 21844263
Well, I partly agree and partly disagree with ghostdog74. Some functionality may be available in the Python standard modules, and then it is better to use, say, os.rename() than to call console command to rename the file. On the other hand, working with shell (or cmd) is the most effective if you have a ready-to-be-used solution.

Until Python 2.4, the simplest way to execute the cmd command was to use os.system(). To avoid showing the console window, you could use os.popen() or the variants os.popen2(), os.popen3(), and popen4() -- depends on what standard files (stdout, stdin, stderr) you want to redirect. See http://docs.python.org/lib/os-newstreams.html#l2h-2626

More modern approach is to use the subprocess module (http://docs.python.org/lib/module-subprocess.html -- see the section on how to replace older os.system() and os.popen() commands)  and its Popen() (http://docs.python.org/lib/node528.html#l2h-3628). You can also use the subprocess.call() helper function.

Notice the default shell=True argument. Then the command is executed via the OS shell. You can also avoid it, but then you have to tell more exact form of arguments (full paths, no "automatic" replacement of shell variable names,...).
0
 
LVL 3

Author Comment

by:Artellos
ID: 21844491
I guess it's a dumb question. (I'm still learning everything in python.)

How would I go about running an ipconfig, save the output to C:\ipconfig.txt without a shell?

I come as far as "subprocess.Popen('ipconfig /all > C:\ipconfig.txt', shell=False)"
However this appears to still show a command screen, what am I doing wrong? ;)

Regards,
Olrik
0
 
LVL 3

Author Comment

by:Artellos
ID: 21844520
I think I'm going to use the older fashion way using os.popen()
Unless there's a really strong argument not to use it ofcourse!

So far os.popen() has worked for me.

Regards,
Olrik
0
 
LVL 29

Expert Comment

by:pepr
ID: 21845245
You can try the following testing script that uses subprocess...
import subprocess
 
fout = open('ipconfig.txt', 'w')
subprocess.Popen('ipconfig /all', stdout=fout)
fout.close()

Open in new window

0
 
LVL 3

Author Closing Comment

by:Artellos
ID: 31469586
Thanks for the help! :)
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Strings in Python are the set of characters that, once defined, cannot be changed by any other method like replace. Even if we use the replace method it still does not modify the original string that we use, but just copies the string and then modif…
Article by: Swadhin
Introduction of Lists in Python: There are six built-in types of sequences. Lists and tuples are the most common one. In this article we will see how to use Lists in python and how we can utilize it while doing our own program. In general we can al…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question