42

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 is because I'm using git and don't 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 path without having to ask in what platform is the script running all the time?

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

3
  • possible duplicate of Platform-independent file paths? Commented Oct 31, 2012 at 16:20
  • 26
    Not a duplicate. Come on guys, give new people a break. Commented Oct 31, 2012 at 16:56
  • 2
    I'm not sure why you think you need an absolute path "because im using git and dont want to deal with excluding all these files". How does that follow? At any rate, it seems like home-relative makes more sense here, because Windows users won't have a directory called C:\var\lib`, so creating C:\var\lib\blob_files` will be weird, but creating the Windows equivalent of ~/.blob_files is perfectly reasonable. Commented Oct 31, 2012 at 18:22

3 Answers 3

54

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
Sign up to request clarification or add additional context in comments.

4 Comments

You don't need abspath here; you need normpath. It's true that abspath implicitly calls normpath, but you should call the right function for the job, not one that happens to work by accident. Also, is this actually going to be on C:, or on the current drive? (And which one is appropriate for the OP's use?)
I chose abspath() intentionally since normpath() doesn't add the drive letter to the returned path. It was hardly an accident. The drive letter will be the one on the current directory where the Python script lives (I'll update my answer accordingly).
doesn't "do the right thing" when the input is a Windows path and you're running on Linux (in other words, it doesn't convert forward slashes to backslashes)
You don't have to change slashes to backslashes.
4

Ok, I got an answer by myself.

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

Maybe it will be useful for anybody

Comments

-1

From Blenders response at Platform-independent file paths?

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

1 Comment

This is not working for me on Windows (using Python 2.7)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.