7

I try to write a script in Python that saves the file in each user directory. Example for user 1, 2 and 3.

C:\Users\user1\Documents\ArcGIS\file1.gdb

C:\Users\user2\Documents\ArcGIS\file1.gdb

C:\Users\user3\Documents\ArcGIS\file1.gdb

How can I do this?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
CesarD
  • 199
  • 1
  • 1
  • 11
  • The `os` and `os.path` modules may be helpful in finding the user directories in `C:\Users`. In particular, `os.listdir` and `os.path.isdir`. – Kevin Jul 25 '13 at 16:17
  • All the answers are very useful, thanks a lot!!! – CesarD Jul 25 '13 at 16:37

3 Answers3

11

As one commenter pointed out, the simplest solution is to use the USERPROFILE environment variable to write the file path. This would look something like:

import os
userprofile = os.environ['USERPROFILE']
path = os.path.join(userprofile, 'Documents', 'ArcGIS', 'file1.gdb')

Or even more simply (with better platform-independence, as this will work on Mac OSX/Linux, too; credit to Abhijit's answer below):

import os
path = os.path.join(os.path.expanduser('~'), 'Documents', 'ArcGIS', 'file1.gdb')

Both of the above may have some portability issues across Windows versions, since Microsoft has been known to change the name of the "Documents" folder back and forth from "My Documents".

If you want a Windows-portable way to get the "Documents" folder, see the code here: https://stackoverflow.com/questions/3858851#3859336

Community
  • 1
  • 1
HardlyKnowEm
  • 3,196
  • 20
  • 30
  • 4
    I believe its not a good practice to augment other answers into yours. This makes other answers useless and redundant. – Abhijit Jul 25 '13 at 16:35
  • 1
    Sorry. Our answers were just different enough--you used a more portable way of getting the User's home directory; I had a discussion of Windows special folders generally--that neither answer covered everything. Since the answer was already accepted, I don't want to delete parts of it, but I will edit it to more clearly attribute the `expanduser` portion to you, and in the future I'll just leave answers be. Sorry again; I still haven't learned all the community practices. I'll take this as a lesson learned. – HardlyKnowEm Jul 25 '13 at 17:08
10

In Python you can use os.path.expanduser to get the User's home directory.

>>> import os
>>> os.path.expanduser("~")

This is a platform independent way of determining the user's home directory.

You can then concatenate the result to create your final path

os.path.join(os.path.expanduser("~"), 'Documents', 'ArcGIS', 'file1.gdb')
Abhijit
  • 62,056
  • 18
  • 131
  • 204
1

You want to use the evironment variable HOME, something like this:

import os
homeDir = os.environ["HOMEPATH"]
file = open(homeDir+"Documents\ArcGIS\file1.gdb")
file.write("Hello, World")
file.close()

Notice that I've used HOMEPATH considering you're using Windows, it may be wrong depending on your OS. Take a look at this: http://en.wikipedia.org/wiki/Environment_variable

Joe Junior
  • 176
  • 5