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?

Mattu475
  • 168
  • 9

2 Answers2

1

Take a look at:

import getpass
user = getpass.getuser()
sys.path.insert(0, '/home/%s/directory'%user)
Mufeed
  • 3,018
  • 4
  • 20
  • 29
1

Try this one:

sys.path.insert(0, os.path.join(os.path.expanduser('~'), 'mydir'))
pnv
  • 2,985
  • 5
  • 29
  • 36