[Python-bugs-list] [ python-Feature Requests-644940 ] Support file path concat with "/"

noreply@sourceforge.net noreply@sourceforge.net
Fri, 29 Nov 2002 01:46:09 -0800

Feature Requests item #644940, was opened at 2002-11-27 21:44
You can respond by visiting: 

Category: Python Interpreter Core
Group: None
Status: Open
Resolution: None
Priority: 5
Submitted By: Mark VanTassel (markvantassel)
Assigned to: Nobody/Anonymous (nobody)
>Summary: Support file path concat with "/" 

Initial Comment:
A very useful feature is to overload the "/" operator for 
strings to support path concatenation - that is, to concat 
the left and right arguments in such a way that there is 
exactly one slash between them, regardless of which 
argument(s) did or didn't include slashes:

dir = "/this/that"
file = "whatever.py"
print dir / file 
#prints "/this/that/whatever.py"

It seems silly, but when you're not 100% in control of 
things (such as when reading paths from config files, 
user input, etc), it's great to be sure that the right thing 
will happen without writing/testing a lot of icky code. 
And even when you are 100% in control, it's nice to not 
have to worry about it so much.

This doesn't seem to conflict with any existing usage or 
syntax, so I don't think it can possibly break any 
existing behaviour (except, of course, those who were 
counting on having an exception thrown!)

I've already coded this as a minor tweak to the Python 
2.2.2 release, and I'd be happy to share the fix with the 
powers that be, if there's general concensus that this is 
a good thing. Feedback is solicited and appreciated 
(this is my first foray into open-source development, so 
be gentle!)


>Comment By: Jack Jansen (jackjansen)
Date: 2002-11-29 10:46

Logged In: YES 

Apart from the question of whether pathname concatenation is important enough to add a special operator on the string type for (I think not), there are lots of boundary issues with your proposal.

For instance, how about a righthandside absolute path? How about two paths with C: style disks in them on Windows? How about Macintosh pathnames (colon-separated, with relative paths starting with a colon)? Or, why does it concatenate pathnames in the first place, and not URLs (makes a difference on Mac)?

If you want to continue down this trail I would suggest the first step is to subclass strings. You can then have separate subclasses for Windows paths, Unix paths, URLs, Mac paths, etc.


Comment By: Mark VanTassel (markvantassel)
Date: 2002-11-27 21:49

Logged In: YES 

P.S. There are a few loose ends for discussion:

1) What to do with empty strings? My current implementation 
doesn't do anything special, so dir/"" ends with a slash, ""/file 
starts with a slash, and ""/"" is a slash. If someone thinks 
different behaviour would be better, I'm open to alternatives.

2) What about back slashes on Windows (my native platform, 
actually)? My version removes trailing slashes and 
backslashes from the 1st arg, and removes leading slashes 
and backslashes from the 2nd ard, and then puts a 
backslash between them... but again I'm open to alternatives.


You can respond by visiting: