Make path to file on Windows works on Linux
Here I have discussed troubles on working on relative path in Python.
This story is dedicate to absolute path, it describes how to make absolute Windows path (with C:\ part) work on Linux.
The source code you can found here. It is available as part of my AlexBerUtils s project.
You can install AlexBerUtils from PyPi:
python3 -m pip install -U alex-ber-utils
See here for more details explanation on how to install.
This is usage example:
Now, I recommend you to put this code snippet after you did your basic logging configuration.
You code should look like something like this:
How fix_retry_env works?
Note: In Python default separator between paths is :
for POSIX and ';'
for Windows, this makes provided path for Windows on POSIX a little tricky. os.pathsep
represents exactly this. So, I will cover 2 basic cases here:
- This is Windows path, we’re running on Windows.
- This is Windows path, we’re running on POSIX.
Note: I’m using “incorrect” slash in Windows path for 2 reasons:
python-dotenv
support only POSIX-type slashes- Windows works fine with “incorrect” slashes, it can successfully resolve them.
This is Windows path, we’re running on Windows.
So, our path looks like C:/app/logs/1.logs,
os.pathsep=';'.
We will correctly split our string to different paths.
For each path, we will check if it found on disk,
if found, we will leave the path unmodified.
Otherwise, we will strip out C:
from the beginning of the path and we will use /app/logs/1.logs
instead.
This is Windows path, we’re running on POSIX.
So, our path looks like C:/app/logs/1.logs,
os.pathsep=':'.
We will incorrectly split our string to different paths. We will get C
and /app/logs/1.logs
as 2 different path to iterate over.
If we have 1-letter path, we will ignore it, so C
will be ignored and we will dial with /app/logs/1.logs
only.
We will check if it found on disk,
if found, we will leave the path unmodified.
Otherwise, we will not found driver information in the beginning of the path, so/app/logs/1.logs
will be used unmodified.
Note: If your path is on D:\
driver, algorithm above may fail. In such case it as advised to do one of the following:
- To use another separator instead of
os.pathsep
(checkdocstring
of fix_retry_env() function how to do it). - Don’t specify driver later inside path (may be Windows will correctly recognize it, I didn’t check) .
- To define mount without driver latter (on POSIX or inside Docker container).
Enjoy!
See also:
importer
module or How to write easily customizable code?fixabscwd()
function inmains
module or Making relative path to file to work.fix_retry_env()
function inmains
module or Make path to file on Windows works on Linux.- parser module or Description of one the oldest AlexBerUtils project
- ymlparsers module or Description of low-level API module for another modules
- init_app_conf module, or My major init_app_con module
- deploys module, or My deploys module
- emails module,or My emails module
- processinvokes module, or My processinvokes module