0

I need to add a directory in home at the beginning of a python program. The problem is that when I'm using different machines I need to edit that line every time to match the right username.

e.g:

sys.path.insert(0, '/home/user_foo/directory')

on computer 1

sys.path.insert(0, '/home/user_bar/directory')

on computer 2

Is there a way in Python3 to get the user name? Or am I doomed to rewrite this line each time I pull it from git on another machine with a different user?

1
  • used this: sys.path.insert(0, os.path.expanduser('~') + '/directory') Commented Jul 11, 2018 at 11:00

2 Answers 2

1

Try this one:

sys.path.insert(0, os.path.join(os.path.expanduser('~'), 'mydir'))
Sign up to request clarification or add additional context in comments.

1 Comment

thanks! I did it a bit different but this way also seems ok.
1

Take a look at:

import getpass
user = getpass.getuser()
sys.path.insert(0, '/home/%s/directory'%user)

2 Comments

Thanks for your answer. Would have marked it as accepted but I used another way to get to the home directory.
That is okay :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.