How to get path to folder on system running python script

Viewed 19

I want to save some files generated by my program to "<drive_name>\Users\<computer_name>\Documents" folder.

I need the drive and computer name. What would be a neat way to achieve this. I usually get the current working directory and just split the string to get that info but there has to be a better way right?

1 Answers

pathlib is more pythonic.

ex)

In [1]: from pathlib import Path

In [2]: path = Path('~').resolve()

In [3]: path
Out[3]: PosixPath('/home/phi/git/python/etl/~')

just declare path = Path(__file__) in your script.

Related