Reading/Writing files

Dan Stromberg drsalists at gmail.com
Fri Mar 25 15:48:46 EDT 2011


On Fri, Mar 25, 2011 at 6:42 AM, Westley Martínez <anikom15 at gmail.com>wrote:

>
> > > I argue that the first is quite a bit more readable than the second:
> > > 'c:/temp/choose_python.pdf'
> > >     os.path.join([ 'c:', 'temp', 'choose_python.pdf' ])
> >
> > I agree with your argument, but think that
> >      r'c:\temp\choose_python.pdf'
> > is even more readable still.  (Also, it wasn't I that suggested using
> > os.path.join() on everything.)
> >
> >
> > > Also, heard of os.path.normpath?  You probably shouldn't compare
> > > pathnames without it.
> >
> > Thanks -- I've heard of it (mostly in the context of resolving relative
> > path names (.. and such)), never checked it out... I'll read up on it.
> >
> > ~Ethan~
>
> Yes, readability counts, but so does platform independence.
>

Last I heard, Python _defined_ forward slash in a pathname as a legal
directory separator - so as long as you normpath when using a pathname so
constructed on its way out of Python and on to the user or external command
(via cmd.exe, command.com, etc.), you are (or were) supposed to be fine.
It's been some time since I heard that though.  References:

http://stackoverflow.com/questions/1607751/how-to-create-a-file-one-directory-up

http://bytes.com/topic/python/answers/23123-when-did-windows-start-accepting-forward-slash-path-separator

http://stackoverflow.com/questions/3020267/how-to-make-a-python-or-perl-script-portable-to-both-linux-and-windows

>From lib/libos.text in the CPython 2.4 source code:
\begin{datadesc}{altsep}
An alternative character used by the operating system to separate pathname
components, or \code{None} if only one separator character exists.  This is
set to \character{/} on Windows systems where \code{sep} is a
backslash.
Also available via \module{os.path}.
\end{datadesc}

And I must confess, those references aren't as convincing as I'd expected.

I don't see anything documenting this behavior beyond it happening to work
fine on *ix and Windows, but I'm just about sure I saw it stated previously
on this mailing list before, that / was legal in Python as a directory
separator on all platforms.

'shame I don't have a MacOS 9, VMS or RiscOS system to test on...  Any
takers?

I'd actually go so far as to say that if only *ix (including MacOS X) and
Windows Python are fine with it, that's not really enough - because I
certainly hope someday there'll be something better than these that Python
will run well on.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110325/72688e2d/attachment-0001.html>


More information about the Python-list mailing list