?
Solved

Hiding the command prompt in my program.

Posted on 2008-06-22
9
Medium Priority
?
1,384 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying 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 2000 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

Installing Python 2.7.3 version on Windows operating system For installing Python first we need to download Python's latest version from URL" www.python.org " You can also get information on Python scripting language from the above mentioned we…
Sequence is something that used to store data in it in very simple words. Let us just create a list first. To create a list first of all we need to give a name to our list which I have taken as “COURSE” followed by equals sign and finally enclosed …
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 modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
Suggested Courses

770 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