[Python-ideas] Support os.path.join for Windows paths on Posix
Steven D'Aprano
steve at pearwood.info
Thu Oct 31 03:56:54 CET 2013
On Wed, Oct 30, 2013 at 05:35:57PM -0500, Ryan Gonzalez wrote:
> The recent thread/post/whatever on os.path.join has gotten me thinking. Say
> I wanted to join a Windows path...on Ubuntu. This is what I get:
[...]
> Isn't something wrong there? My idea: check for \'s in the path. If there
> are any, assume \ is the path separator, not /.
Others have already pointed out that using ntpath is the right solution
for this problem, but I wanted to mention that the suggestion to guess
the path separator based on the path is risky. Backslashes are legal in
file and directly names on POSIX systems (Mac, Unix, Linux etc), which
means that sometimes os.join will be given a POSIX path containing
backslashes, in which case it will wrongly guess it is a Windows path.
os.path.join("ab\\c", "d", "file.txt")
on POSIX ought to give the path containing two directories and a file
name 'ab\\c/d/file.txt' but with the guesser will generate a single file
name 'ab\\c\\d\\file.txt'.
--
Steven
More information about the Python-ideas
mailing list