Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python save files to user folder (Windows)

I have this program in Python which should save text files to a folder inside the "My Documents" of the user. The problem is that the program doesn't know the username, or the Windows version to know the correct path. How could I get this done?

like image 669
marcocamejo Avatar asked Dec 19 '25 11:12

marcocamejo


1 Answers

The doc says, the following code is expanded to the user's home directory on windows too..

homeDir = os.path.expanduser("~")

From the documentation:

On Windows, HOME and USERPROFILE will be used if set, otherwise a combination of HOMEPATH and HOMEDRIVE will be used. An initial ~user is handled by stripping the last directory component from the created user path derived above.


Also look into this for using Win32APIs

like image 119
UltraInstinct Avatar answered Dec 21 '25 23:12

UltraInstinct



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!