0

I have two folders. Call them A and B.

A is populated with python code. Among them, function1.py.

B contains data, and a soft link (created using ln -s) to A/function1.py.

When I execute B/function1.py, pwd points to A, not B. How do I fix this?

asked Jul 13, 2024 at 17:26
2
  • What do you mean by "pwd points to A, not B"? Do you call pwd Linux command from within function1.py? When you execute B/function1.py, you're actually executing A/function1.py, since B/function1.py is just a symbolic link. Commented Jul 13, 2024 at 17:41
  • The working directory (pwd) is the directory where you call the script from, not the path to the script. If you are in /var/run and you call /B/function1.py, the working directory is /var/run. The working directory doesn't change if the script is a soft link. You can change your current working directory with cd ../B before you run the script or you can change the working directory inside the script with os.chdir(path). Commented Jul 13, 2024 at 17:51

1 Answer 1

-1

Removing the script name from the __file__

Solution 1: path: str = os.path.dirname(__file__)

Solution 2: path: str = os.path.sep.join(__file__.split(os.path.sep)[:-1])


Solution 1 works by just getting the parent directory of the current script path. Solution 2 works by removing the script name from the full script path. Also, if you're using windows, replacing os.altsep with os.sep in __file__ may be required!

Resolving softlinks in your path:

Check out the os.path package, there you will probably find a function for that. I am not really sure, but maybe try using os.path.realpath? If not, try os.path.abspath.

note

I didn't test any of this sadly cuz i am lazy, so it may not work!

answered Jul 13, 2024 at 17:34
Sign up to request clarification or add additional context in comments.

1 Comment

The question is about the working directory, not about the file path.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.