[plug] [OT] python and spaces in path strings

Craig Ringer craig at postnewspapers.com.au
Wed Dec 22 11:48:56 WST 2004


Russell Steicke wrote:
> On Wed, Dec 22, 2004 at 10:53:55AM +0800, rpowersau at gmail.com wrote:
> ...
> 
>>If I enter a path on the command line, and the path has spaces, when
>>that path gets sent to the remote site I get a 404 error. But if I
>>type in %20 instead of spaces it works. Not that bad except I'm using
>>that path to create a local directory. If I type in %20 then the local
>>directory becomes "firstname%20lastname" instead of "firstname
>>lastname".
> 
> 
> You want urllib.pathname2url(path) and urllib.url2pathname(path).

Yep. If you run into other situations where there aren't such handy 
built-in tools, you can use str.replace instead:

escaped_string = "firstname lastname".replace(" ","%20")

however in this case Russell's approach is much better.

--
Craig Ringer



More information about the plug mailing list