I am opening a file in my python code. For a particular user, the path is /home/user_name/IB/MAIN where user_name is the personal user name. For testing and stuff I have used my own user name but if I give this code to someone else, it won't run as the path is different for that user, so how do I get a generic path for this file?
Asked
Active
Viewed 581 times
-1
-
you will find your answer [here](https://stackoverflow.com/questions/4028904/how-to-get-the-home-directory-in-python) or [here](https://stackoverflow.com/questions/2668909/how-to-find-the-real-user-home-directory-using-python) – Advay Umare Feb 05 '18 at 05:17
1 Answers
0
Use
'~/IB/MAIN'
to denote relative path to user home.
or
import os
homepath = os.getenv("HOME")
path = os.path.join(homepath, "IB/MAIN")
via environment variable

John R
- 1,505
- 10
- 18
-
how does this give me the path to that file say "something.exe"? – Rajakulasekhar Ramakrishnan Feb 05 '18 at 05:43
-
A shortcut method `Path.home()` is provided [link](https://docs.python.org/3/library/pathlib.html#pathlib.Path.home) – SpringMaple Feb 05 '18 at 05:51