Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using absolute unix paths in windows with python

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:\var\lib\blob_files\myfile.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

like image 813
jeruki Avatar asked Oct 31 '12 16:10

jeruki


People also ask

How do you pass a file path as an argument in Python?

To use it, you just pass a path or filename into a new Path() object using forward slashes and it handles the rest: Notice two things here: You should use forward slashes with pathlib functions. The Path() object will convert forward slashes into the correct kind of slash for the current operating system.

How do you use absolute path instead of relative path in Python?

Python: Absolute Path vs. Relative Path. An absolute path is a path that describes the location of a file or folder regardless of the current working directory; in fact, it is relative to the root directory. A relative path that depicts the location of a file or folder is relative to the current working directory.


Video Answer


1 Answers

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:\var\lib\blob_files\myfile.blob  print os.path.abspath(os.path.expanduser("~/blob_files/myfile.blob")) >>> C:\Users\jerry\blob_files\myfile.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 
like image 112
kindall Avatar answered Sep 21 '22 15:09

kindall