0

So, my idea is, if I just throw a python script into a folder, for example this:

path = r"C:\Users\[name]\Downloads"

print("This:", path, "is your path")

Can I tell Python to find the folder the script is in, and fill the path variable by itself, without having to manually add the path?

I've tried with these:

os.path.abspath(os.getcwd())

and

"\n".join(sys.path)

But didn't get the results I want.

*Information:

  • I'm using Python 3.9
  • Windows 10*
1
  • It would be nice if you could add a sample response in your question. Commented Feb 20, 2022 at 22:59

1 Answer 1

1

I use pathlib library to deal with path-related operations in python3.X.

You can try this :

from pathlib import Path

path = Path(__file__).parent.absolute()

You can read more about the library here : https://docs.python.org/3/library/pathlib.html

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.