• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1393
  • Last Modified:

Hiding the command prompt in my program.

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
Artellos
Asked:
Artellos
  • 5
  • 2
  • 2
1 Solution
 
ghostdog74Commented:
what is it that you need to call a shell command, that Python cannot do? A third party application ?
0
 
ArtellosAuthor Commented:
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
 
ghostdog74Commented:
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
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 
ArtellosAuthor Commented:
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
 
peprCommented:
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
 
ArtellosAuthor Commented:
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
 
ArtellosAuthor Commented:
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
 
peprCommented:
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
 
ArtellosAuthor Commented:
Thanks for the help! :)
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 5
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now