Using absolute unix paths in windows with python

Question:

I’m creating an application that stores blob files into the hard drive, but this script must run in both linux and windows, the issue is that i want to give it an absolute path from the filesystem root and not one relative to the project files, this because im using git and dont want to deal with excluding all these files from syncing.

So i would like to have something like this:

path = '/var/lib/blob_files/'
file = open(path+'myfile.blob', 'w')

and get a file in unix at:

/var/lib/blob_files/myfile.blob

and in windows at:

C:varlibblob_filesmyfile.blob

it could also be relative to the user home folder (/home/user in unix and C:/Users/User in windows) but i guess the problem is very similar.

How can i achieve this? is there any library or function that can help me transparently to convert this paths without having to ask in what plataform is the scrip running all the time?

Of my two options, absolute from root or relative from home folder, which one do you recomend to use?

Thanks in advance for any advice on this

Asked By: jeruki

||

Answers:

From Blenders response at Platform-independent file paths?

>>> import os
>>> os.path.join('app', 'subdir', 'dir', 'filename.foo')
'app/subdir/dir/filename.foo'
Answered By: dm03514

Use os.path.abspath(), and also os.path.expanduser() for files relative to the user’s home directory:

print os.path.abspath("/var/lib/blob_files/myfile.blob")
>>> C:varlibblob_filesmyfile.blob

print os.path.abspath(os.path.expanduser("~/blob_files/myfile.blob"))
>>> C:Usersjerryblob_filesmyfile.blob

These will “do the right thing” for both Windows and POSIX paths.

expanduser() won’t change the path if it doesn’t have a ~ in it, so you can safely use it with all paths. Thus, you can easily write a wrapper function:

import os
def fixpath(path):
    return os.path.abspath(os.path.expanduser(path))

Note that the drive letter used will be the drive specified by the current working directory of the Python process, usually the directory your script is in (if launching from Windows Explorer, and assuming your script doesn’t change it). If you want to force it to always be C: you can do something like this:

import os
def fixpath(path):
    path = os.path.normpath(os.path.expanduser(path))
    if path.startswith("\"): return "C:" + path
    return path
Answered By: kindall

Ok, I got an answer by myself.

os.path.exists(os.path.abspath(filePath))

Maybe it will be useful for anybody

Answered By: Snorlax

I wanted to convert simply from ‘/’ to ” in the python code. I have one shared folder ‘test_folder’ from windows PC (IP: 192.168.1.1). I had used os, pathlib and ntpath libraries. But, all gave result with double ”. I just wanted path to be converted to windows-like path with single ”. Below solution using re module solved the problem.

import re
unix_path = '//192.168.1.1/test_folder/'
win_path = re.sub(r'/', r'\', unix_path)
print(win_path)
Answered By: Alex