Solved

Hiding the command prompt in my program.

Posted on 2008-06-22
9
1,330 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
  • 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 28

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 28

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Flask is a microframework for Python based on Werkzeug and Jinja 2. This requires you to have a good understanding of Python 2.7. Lets install Flask! To install Flask you can use a python repository for libraries tool called pip. Download this f…
Dictionaries contain key:value pairs. Which means a collection of tuples with an attribute name and an assigned value to it. The semicolon present in between each key and values and attribute with values are delimited with a comma.  In python we can…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
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…

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now