Helio
Helio

Reputation: 1

Can Python find path automatically?

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:

Upvotes: 0

Views: 266

Answers (1)

Prashant Kumar
Prashant Kumar

Reputation: 2092

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

Upvotes: 1

Related Questions