2020/08/01

Use python's pathlib to implement file path across OS

Why file path is an issue across platform

Nowadays, there are 3 common OS, which are Windows, Linux, and MacOS. Since Linux, and MacOS shares same file path’s implementation, developers do not need to worry about file path issues on between them. The problem is between Windows, and others.
In Windows, file path can be expressed in either 2 formats:
  • \folder1\folder2\file1.txt (Window format)
  • /folder1/folder2/file1.txt (POSIX format)
The difference is the direction of the slash. A side note is that file path in Linux / MacOS is expressed in POSIX format ONLY.
Actually, if applications in Windows can eat them both properly, I don’t need to write this blog since I can simply written in 2nd format at all. According to my experiences, even applications in Windows accepts either one of them only. It is a try and error process for picking a correct format for each applications.
Back to the title of this blog, file path issue will be bigger if your implementation runs across platform. For example, a library (Python zipfile library) may accepts POSIX format but the format of input file path may be Window format at all. So, the question is whether python provide library for us to do this conversion or not.
A side note on python zipfile. This library accepts both kind of formats since py3.8

pathlib

Pathlib is a builtin library for us to fix this problem. Below demonstrates 2 ways on conversion via such library. For other advance usage, please consult the library here.

Get POSIX file path from different input format

import pathlib
winPath = r'\workspace\xxx\test_fixture\user-restore-success.zip'
posixPath = '/workspace/xxx/test_fixture/user-restore-success.zip'
pWIN = pathlib.PureWindowsPath(winPath)
pPOSIX = pathlib.PureWindowsPath(posixPath)
pWIN.as_posix()
#'/workspace/xxx/test_fixture/user-restore-success.zip'
pPOSIX.as_posix()
#'/workspace/xxx/test_fixture/user-restore-success.zip'

Get Window file path

str(pWIN)
str(pPOSIX)

Notes

  • Always favor PureWindowsPath when doing conversion
PureWindowsPath is able to convert between Window path, and POSIX path. PurePosixPath, on the other hand, is not able to do so since Backslash () is a valid filename in POSIX path.
For detail, please refer to the discussions here
  • Bug in Path.resolve() on Windows platform
According to here, Path.resolve() in windows cannot return the absolute file path if such file is not existed at first. If you need a reliable way to get absolute path of a file right now, use os.path.abspath instead

沒有留言:

張貼留言