[Tutor] White spaces in a path (from a string variable) and os.popen on Windows
learner404
learner404 at gmail.com
Thu Mar 8 15:52:52 CET 2007
Hello,
I'm getting crazy with white spaces on Windows. :(
I'm aware of the three paths gotchas on Windows well explained here:
http://www.freenetpages.co.uk/hp/alan.gauld/tutfiles.htm
But my problem is with white spaces in windows paths (very common with
"program files", "documents and settings",etc).
I precise I use os.popen because I need to read the answer from an external
command-line app that I feed with a file or folder path.
When I know exactly the path in advance I use a raw string r' ' and I add
double quotes where necessary to avoid the white spaces syndrome. But I
don't see how to do this "easily" when my path is a string variable given by
my GUI (like C:\Documents and Settings\name\Mes documents)
I've googled this problem but wasn't successful so far. I just seem to find
people who have problems with whitespaces and Python like here:
http://e-scribe.com/news/324
Can you help me to understand how to deal with white spaces in Pyhton (or
SVP point me to any URL or relevant book). :)
For now I'm trying to make work a minimal "hello world" example here :
The little script below shows a button (wx) to select a directory.
I print in the console the selected directory (string) and I try to recover
the result of a DOS dir command thought an os.popen.
If I select a folder without a whitespace(s) it works well. If the path has
a whitespace it doesn't (not apparent result).
Can you make this script work in all situation on Windows ? (ie whatever the
folder I select with the GUI) ?
Many, many thanks in advance. :)
# Test with popen and paths containing white spaces
import wx,os,sys
app=wx.App(redirect=False)
class GUI(wx.Frame):
def __init__(self,parent,title):
wx.Frame.__init__(self, parent, -1, title,size=(400,100))
bkg=wx.Panel(self)
dirButton=wx.Button(bkg,label="Select a directory")
self.Bind(wx.EVT_BUTTON, self.goPath, dirButton)
def goPath(self,evt):
myDir=wx.DirDialog(self)
myDir.ShowModal()
dirPath=myDir.GetPath()
print "GetPath result is",dirPath
result=os.popen("dir "+dirPath).read()
print "os.popen result is ",result
win=GUI(None,title="Test")
win.Show()
app.MainLoop()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20070308/d240ce4e/attachment.htm
More information about the Tutor
mailing list