[Tutor] path

Mats Wichmann mats at wichmann.us
Sat Jun 29 09:42:00 EDT 2019


On 6/29/19 6:46 AM, ingo wrote:
> A user has to type a path in the commandline on Win 10, so just a
> string.
> A short excerpt:
> 
> Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64
> bit (AMD64)] on win32
> Type "copyright", "credits" or "license()" for more information.
>>>> inp = "c:\test\drive\this"
>>>> import pathlib
>>>> p=pathlib.PurePath(inp)
>>>> p
> PureWindowsPath('c:\test/drive\this')
>>>> print(pathlib.PurePosixPath(p))
> c:/ est/drive his
>>>> inp
> 'c:\test\\drive\this'
>>>> import os
>>>> print(os.path.normpath(inp))
> c:  est\drive his
>>>> print(pathlib.Path(inp))
> c:  est\drive his
>>>>
> 
> how to go from a string to a path, how to append to a path (os.path.join
> or / with Path), how to turn it into 'posix'

Most people don't use pathlib, and that's kind of sad, since it tries to
mitigate the kinds of questions you just asked. Kudos for trying.

For your example, when you define inp as a string, it needs to be a raw
string because otherwise Python will interpret the backslash sequences.
\t means tab, which is why the the results look mangled.

inp = "c:\test\drive\this"

If you're going to use pathlib, then may as well use the / operator for
joining,



More information about the Tutor mailing list