On 04/07/18 21:30, Chris Angelico wrote:
On Thu, Jul 5, 2018 at 6:25 AM, Mikhail V <mikhail...@gmail.com> wrote:
Joe Pfeiffer wrote:
On Windows a path is e.g.:
C:\programs\util\
So what is reasonable about using forward slashes?
It happens to me that I need to copy-paste real paths like 100 times
a day into scripts - do you propose to convert to forward slashes each time?
That's what started the thread -- using backslashes caused a \a to be
interpreted as a special character instead of two characters in the
path.
Yes, and the answer was a week ago: just put "r" before the string.
r"C:\programs\util"
And it worked till now. So why should I replace backslashes with
forward slashes?
There is one issue that I can't write \ on the end:
r"C:\programs\util\"
But since I know it's a path and not a file, I just write without trailing \.
That's exactly the issue. But if you just always separate paths with
forward slashes, you never have a problem. There is no "replace"
happening; you simply use the correct path separation character from
the start. You can assemble strings from pieces without having to
remember to slap an "r" in front of each literal; you can have slashes
at the ends of strings; everything will just work.
ChrisA
Maybe it would be smarter to build paths using os.sep, os.path.join and
os.path.split rather than use literal strings. There is also os.altsep
https://docs.python.org/3/library/os.html#os.altsep which if I'd heard
about I'd forgotten about :) And others. Failing that there's always
pathlib https://docs.python.org/3/library/pathlib.html
--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.
Mark Lawrence
--
https://mail.python.org/mailman/listinfo/python-list