[Tutor] slashes in paths

Dominik George nik at naturalnet.de
Sat Jul 20 22:37:30 CEST 2013


Hi Jim,

> But oddly, it makes all slashes forward if I end the path with a
> forward slash, so it's not consistent with itself.

It is, in the sense that it preserves any form of seperator that is
already there. It just doesn't throw away what you want to be there:

 >>> import ntpath
 >>> ntpath.join("a", "b")
 'a\\b'
 >>> ntpath.join("a/", "b")
 'a/b'
 >>> ntpath.join("a/", "b", "c")
 'a/b\\c'
 >>> 

"Explicit is better than implicit", so if you explicitly require it to
use a certain seperator it uses it. If you don't, it implie the
OS-native seperator. Remember that Windows NT is alright with forward
slashes, even outside Python, so it is ok to preserve it if it is
already there.

(On a side note, I imported ntpath to force the NT pathname behaviour on
my Linux Python. os.path always matches the *path module for the current
OS, so there is no difference there.)

> BTW, is ending a path with a slash a Linux requirement, or just a
> convention, or a who-cares-either-way? I forget since I've been on
> windows a long time, and it accepts either method. I guess the best
> thing is to always end all paths with a forward slash, catching both
> OS's and being consistent.

It is a POSIX convention. I think a POSIX OS is allowed to rais ENOENT
if you leave the last slash out when referencing a directory, but most
shells and programs imply it.

-nik

-- 
<Natureshadow> Auf welchem Server liegt das denn jetzt…?
<mirabilos> Wenn es nicht übers Netz kommt bei Hetzner, wenn es nicht
            gelesen wird bei STRATO, wenn es klappt bei manitu.

PGP-Fingerprint: 3C9D 54A4 7575 C026 FB17  FD26 B79A 3C16 A0C4 F296
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 905 bytes
Desc: Digital signature
URL: <http://mail.python.org/pipermail/tutor/attachments/20130720/783b9b31/attachment-0001.pgp>


More information about the Tutor mailing list