2

Long-time veteran of C# and Swift, but new to Python so forgive me if this is a basic question.

I'm trying to solve a seemingly simple problem. We have a Python3 function that takes a list of directories represented as a string array. We're using those values in some outputted JSON and in that JSON, it has to have a guaranteed format.

What we're trying to address is the following case (Note: This will either be on macOS or Linux):

File 1: /Users/Joe/Some/Path/To/Folder/
File 2: /Users/Joe/Some/Path/To/Folder   <-- Note the missing trailing slash
File 3: ~/Some/Path/To/Folder/           <-- Note the 'home' tilde

All three of the above should output a format matching File 1 above...

/Users/Joe/Some/Path/To/Folder/

Well I can do this with regex and/or string manipulation, I’m hoping there’s something in a standard library‘s that I can use, especially to get platform-independence where on some systems things are forward-slashes and others they are back-slashes.

Any ideas?

Mark A. Donohoe
  • 28,442
  • 25
  • 137
  • 286
  • What do you expect of a ``FileInfo``-like object? Are you aware of the standard library ``pathlib`` and ``pathlib.Path``? – MisterMiyagi Oct 09 '20 at 15:32
  • I'm not interested in the FileInfo itself. I'm more interested in getting normalized string representations and I was trying to avoid manually checking for trailing slash characters, etc. That object in other languages encapsulates all that logic for me so I don't have to worry about it. – Mark A. Donohoe Oct 09 '20 at 15:34
  • I'm mainly asking because I'm wondering whether you expect the program to detect whether the path points to a directory, or should just unconditionally append ``/``. – MisterMiyagi Oct 09 '20 at 15:42
  • Definitely the former. That's the entire point! BUT... I just found `os.path.abspath` which seems to take all three above and always outputs #2, so if I use that, I can just append a slash! – Mark A. Donohoe Oct 09 '20 at 15:44

1 Answers1

2

The canonical tools for handling paths are the pathlib and low-level os.path standard libraries. Both handle paths in a platform independent way, though it is customary to use the UNIX convention of / path separator. pathlib additionally supports platform specific behaviour by explicitly using pathlib.PosixPath/pathlib.PurePosixPath or pathlib.WindowsPath/pathlib.PureWindowsPath; the Pure... variants can be used cross-platform.


Using pathlib allows to explicitly .expanduser() and optionally .resolve() it given logical or actual file system operations. A pathlib.Path always removes trailing separators; if a trailing separator is desired (e.g. to indicate directory or link handling), it must be added explicitly.

import random
import pathlib

raw_path = random.choice(['/Users/Joe/Some/Path/To/Folder/', '/Users/Joe/Some/Path/To/Folder', '~/Some/Path/To/Folder/'])

path = pathlib.Path(raw_path).expanduser().resolve()  # normalize path
rawdir_path = str(path/'-')[:-1]                      # format with trailing separator
print(rawdir_path)
MisterMiyagi
  • 44,374
  • 10
  • 104
  • 119