Leo Jenkins
Leo Jenkins

Reputation: 11

Navigate to file without username

I am trying to make a program that will move a file into the startup dir and run code, on windows 7 the startup folder is past c:\Users[Username] in my case I wont know that when the code is being run.

Thanks.

Upvotes: 1

Views: 51

Answers (1)

Ignatius
Ignatius

Reputation: 3312

In Windows, the environment variable USERPROFILE points to the user folder. In python, you can use this like:

import os
user_path = os.environ['USERPROFILE']
print(user_path)

Note that non-Windows systems usually don't have USERPROFILE, they use HOME. If you are sticking to Windows you can use USERPROFILE, but if you want to make your program platform-independent, you can do the following.

import os
user_path = os.path.expanduser('~')
print(user_path)

Upvotes: 1

Related Questions