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

  • Home
  • SEARCH
  • 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 50543
In Process

The Archive Base Latest Questions

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

In python is it possible to get or set a logical directory (as opposed

  • 0

In python is it possible to get or set a logical directory (as opposed to an absolute one).

For example if I have:

/real/path/to/dir 

and I have

/linked/path/to/dir 

linked to the same directory.

using os.getcwd and os.chdir will always use the absolute path

>>> import os >>> os.chdir('/linked/path/to/dir') >>> print os.getcwd() /real/path/to/dir 

The only way I have found to get around this at all is to launch ‘pwd’ in another process and read the output. However, this only works until you call os.chdir for the first time.

  • 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-10T16:36:36+00:00Added an answer on May 10, 2026 at 4:36 pm

    The underlying operational system / shell reports real paths to python.

    So, there really is no way around it, since os.getcwd() is a wrapped call to C Library getcwd() function.

    There are some workarounds in the spirit of the one that you already know which is launching pwd.

    Another one would involve using os.environ['PWD']. If that environmnent variable is set you can make some getcwd function that respects it.

    The solution below combines both:

    import os from subprocess import Popen, PIPE  class CwdKeeper(object):     def __init__(self):         self._cwd = os.environ.get('PWD')         if self._cwd is None: # no environment. fall back to calling pwd on shell            self._cwd = Popen('pwd', stdout=PIPE).communicate()[0].strip()         self._os_getcwd = os.getcwd         self._os_chdir = os.chdir      def chdir(self, path):         if not self._cwd:             return self._os_chdir(path)         p = os.path.normpath(os.path.join(self._cwd, path))         result = self._os_chdir(p)         self._cwd = p         os.environ['PWD'] = p         return result      def getcwd(self):         if not self._cwd:             return self._os_getcwd()         return self._cwd  cwd = CwdKeeper() print cwd.getcwd() # use only cwd.chdir and cwd.getcwd from now on.     # monkeypatch os if you want: os.chdir = cwd.chdir os.getcwd = cwd.getcwd # now you can use os.chdir and os.getcwd as normal. 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 106k
  • Answers 106k
  • 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 It's actually really easy to do :-) SqlDbType is an… May 11, 2026 at 8:58 pm
  • Editorial Team
    Editorial Team added an answer "Does Java load an entire .jar file into memory when… May 11, 2026 at 8:58 pm
  • Editorial Team
    Editorial Team added an answer At my workplace (small consultancy company with about 10 developers,… May 11, 2026 at 8:58 pm

Related Questions

Implementing a 'sandbox' environment in Python used to be done with the rexec module
This is a two part question. A dumb technical query and a broader query
When I try this code: a, b, c = (1, 2, 3) def test():
Is it possible to perform a named-group match in Perl's regex syntax as with

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.