[Tutor] Regex for Filesystem path (Asad)
Peter Otten
__peter__ at web.de
Thu Nov 8 11:53:00 EST 2018
Albert-Jan Roskam wrote:
> I was thinking it would also be possible to do (in Windows):
> import os.path
> os.path.sep = '/'
> os.path.normpath('c:\\beeh/foo\\bar/baz')
>
> But alas, this still creates normalized windows-style paths.
If your input data has only forward slashes you can keep it that way with
posixpath, i. e.
>>> import posixpath
>>> posixpath.join("foo", "bar")
'foo/bar'
should produce the same result on windows.
posixpath.normpath() cannot replace backward with forward slashes because
the backslash is an ordinary character on Linux et. al.
Fun fact: scripts written with Windows in mind sometimes work on Linux, with
the output going to unexpected places:
$ python3
Python 3.4.3 (default, Nov 28 2017, 16:41:13)
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> with open("c:\\foo\\bar.txt", "w") as f: print("hi", file=f)
...
>>>
$ ls
c:\foo\bar.txt
More information about the Tutor
mailing list