I have recently noticed some problems with the operation
of popen os streams, and I have reason to believe that it might be related to
my use of wxPython. I have been using a simple wxPython app to perform the simple
task of pulling up a file dialog and returning the filename selected. This app is
run via a function call in other modules or maybe from the python shell command
line. Here is an example of what is going wrong:
I can open an OS command stream, read from it, then
close it, successfully, until I run the aforementioned wxPython app once.
Thereafter, I always get error 1 returned when I close the popen stream.
So, I can do the following all day long with no
errors (close() return None on success):
f=os.popen(“dir”); r=f.read(); print
f.close()
But when I run my app, and then do:
f=os.popen(“dir”); r=f.read(); print
f.close()
it prints out 1.
I am running WindowsXP, Python 2.3.3, wxPython 2.5.1.5.
My app is run within the scope of function
wxGetFilename. See below for details. Any ideas why the popen errors?
Thanks,
T
···
wxGlobals.py
This module defines wrappers for commonly-used
GUIs
implemented in wxPython.
import wx
import os.path
class App_GetFilename(wx.App):
'''This is the wxApp that
implements the wxGetFilename wrapper.’’’
def __init__(self,
DialogMessage=“Choose A File”,
DefaultDir="",
DefaultFile="",
Wildcard=“All files (.)|.”,
Style=wx.OPEN,
RememberLastDir=True):
# set
members needed by the wx App
self.DialogMessage = DialogMessage
self.DefaultDir = DefaultDir
self.DefaultFile = DefaultFile
self.Wildcard = Wildcard
self.Style = Style
self.RememberLastDir = RememberLastDir
self.Filename = “”
self.Dirname
= “”
# init
the wx App - Note: this calls OnInit
wx.App.init(self)
def OnInit(self):
frame =
wx.Frame(None, -1, “Dummy Frame”,
pos=(5,5), size=(100,100))
frame.Show(False)
self.SetTopWindow(frame)
if
self.RememberLastDir :
self.Style |= wx.CHANGE_DIR
self.dlg
= wx.FileDialog(parent=frame,
message=self.DialogMessage,
defaultDir=self.DefaultDir,
defaultFile=self.DefaultFile,
wildcard=self.Wildcard,
style=self.Style)
self.dlg.Raise()
if
self.dlg.ShowModal() == wx.ID_OK:
self.Filename = self.dlg.GetFilename()
self.Dirname = self.dlg.GetDirectory()
# Done
with GUI, important data now in this app obj
# Now
clean up the GUI!
self.dlg.Destroy()
frame.Destroy()
return
True
def wxGetFilename(DialogMessage=“Choose a
file”,
DefaultDir="",
DefaultFile="",
Wildcard=“All files (.)|.|” \
“XML files (.xml)|.xml”,
Style=wx.OPEN,
RememberLastDir=True) :
'''Runs a wxApp that creates a
wxFileDialog according to
the specified function
parameters. When RememberLastDir
is True, and DefaultDir is an
empty string, the dialog
uses the last directory selected
as the default directory
for subsequent selections.
DefaultFile is the default file
that is selected in the dialog.
Wildcard is a string that
contains the description of the
wildcard filter to use in
the dialog. Style specifies
whether it should be a “Open”
(use wxOPEN) or a
“Save” (use wxSAVE) file dialog. The
function returns the full
absolute path of
the filename that is selected in
the dialog. If the dialog
is canceled, an empty string is
returned.’’’
app =
App_GetFilename(DialogMessage,
DefaultDir,
DefaultFile,
Wildcard,
Style,
RememberLastDir)
app.MainLoop()
FullPath =
os.path.join(app.Dirname, app.Filename)
return os.path.abspath(FullPath)