Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8765249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:14:06+00:00 2026-06-13T16:14:06+00:00

I’m creating an application that stores blob files into the hard drive, but this

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T16:14:07+00:00Added an answer on June 13, 2026 at 4:14 pm

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have a small JavaScript validation script that validates inputs based on Regex. I
This could be a duplicate question, but I have no idea what search terms
I know there's a lot of other questions out there that deal with this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.