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 84199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T21:53:36+00:00 2026-05-10T21:53:36+00:00

In relation to another question , how do you account for paths that may

  • 0

In relation to another question, how do you account for paths that may change? For example, if a program is calling a file in the same directory as the program, you can simply use the path ‘.\foo.py’ in *nix. However, apparently Windows likes to have the path hard-coded, e.g. ‘C:\Python_project\foo.py’.

What happens if the path changes? For example, the file may not be on the C: drive but on a thumb drive or external drive that can change the drive letter. The file may still be in the same directory as the program but it won’t match the drive letter in the code.

I want the program to be cross-platform, but I expect I may have to use os.name or something to determine which path code block 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. 2026-05-10T21:53:36+00:00Added an answer on May 10, 2026 at 9:53 pm

    Simple answer: You work out the absolute path based on the environment.

    What you really need is a few pointers. There are various bits of runtime and environment information that you can glean from various places in the standard library (and they certainly help me when I want to deploy an application on windows).

    So, first some general things:

    1. os.path – standard library module with lots of cross-platform path manipulation. Your best friend. ‘Follow the os.path’ I once read in a book.
    2. __file__ – The location of the current module.
    3. sys.executable – The location of the running Python.

    Now you can fairly much glean anything you want from these three sources. The functions from os.path will help you get around the tree:

    • os.path.join('path1', 'path2') – join path segments in a cross-platform way
    • os.path.expanduser('a_path') – find the path a_path in the user’s home directory
    • os.path.abspath('a_path') – convert a relative path to an absolute path
    • os.path.dirname('a_path') – get the directory that a path is in
    • many many more…

    So combining this, for example:

    # script1.py # Get the path to the script2.py in the same directory import os this_script_path = os.path.abspath(__file__) this_dir_path = os.path.dirname(this_script_path) script2_path = os.path.join(this_dir_path, 'script2.py') print script2_path 

    And running it:

    ali@work:~/tmp$ python script1.py  /home/ali/tmp/script2.py 

    Now for your specific case, it seems you are slightly confused between the concept of a ‘working directory’ and the ‘directory that a script is in’. These can be the same, but they can also be different. For example the ‘working directory’ can be changed, and so functions that use it might be able to find what they are looking for sometimes but not others. subprocess.Popen is an example of this.

    If you always pass paths absolutely, you will never get into working directory issues.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 85k
  • Answers 85k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Take a look at PrintStream's source. It has two references… May 11, 2026 at 5:15 pm
  • Editorial Team
    Editorial Team added an answer The following code replicates the configuration in my original question:… May 11, 2026 at 5:15 pm
  • Editorial Team
    Editorial Team added an answer Use BeginInvoke: delegate void sort(); private void treeView1_AfterLabelEdit(object sender, NodeLabelEditEventArgs… May 11, 2026 at 5:15 pm

Related Questions

Before you answer this I have never developed anything popular enough to attain high
I am using CodeIginter as my PHP framework. I am explaining the situation below:
I'm trying to wrap my head around xml schemas and one thing I'm trying
I have started using Linq to SQL in a (bit DDD like) system which

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.