0

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?

Tony
  • 76
  • 6
  • 1
    You could make use of environment variables to get the path to the `User` folder and the computer name. This might help: https://stackoverflow.com/questions/4906977/how-do-i-access-environment-variables-in-python and https://learn.microsoft.com/en-us/windows/deployment/usmt/usmt-recognized-environment-variables – majusebetter Sep 08 '22 at 08:46
  • e.g. `os.environ['COMPUTERNAME']`. https://stackoverflow.com/questions/799767/getting-name-of-windows-computer-running-python-script – majusebetter Sep 08 '22 at 08:52

1 Answers1

0

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.

phi friday
  • 191
  • 4