Autopopulate username using Python

3443
6
Jump to solution
08-01-2014 04:40 PM
Sheri_AnneSantos
New Contributor II

Hi, i am trying to use row.setValue to set the field value of my username column to my username. Is there another way i can automate this with python as my code below is not working:

row.setValue("INTERNAL_ROW_CREATED_BY", userName())

0 Kudos
1 Solution

Accepted Solutions
XanderBakker
Esri Esteemed Contributor

If you look at the thread on stack overflow:

portability - Is there a portable way to get the current username in Python? - Stack Overflow

... there are some examples provided to get the current user name which worked on my system (Windows 8.1, Python 2.7):

import os

print os.environ["USERNAME"]

print os.path.split(os.path.expanduser('~'))[-1]

import getpass

print getpass.getuser()

They all printed my current user name.

Kind regards,

Xander

View solution in original post

6 Replies
DanPatterson_Retired
MVP Emeritus

Can you post all the code, one line out of an updatecursor isn't going to provide much information

0 Kudos
Sheri_AnneSantos
New Contributor II

Hi. Here is the code:

# Import all necessary modules

import os, sys, arcpy

from arcpy import env

outpath = "C:/Users/ssantos/Desktop/Overarching/Vector/TEMP.gdb/"

# Get the information you need to populate temporary table

a = arcpy.GetParameterAsText(0).upper()

b = arcpy.GetParameterAsText(1).upper()

c = arcpy.GetParameterAsText(2)

d = arcpy.GetParameterAsText(3).upper()

e = arcpy.GetParameterAsText(4)

f = arcpy.GetParameterAsText(5).upper()

g = arcpy.GetParameterAsText(6).upper()

h = arcpy.GetParameter(7)

i = arcpy.GetParameter(8)

j = arcpy.GetParameter(9)

k = arcpy.GetParameter(10)

l = arcpy.GetParameter(11)

m = arcpy.GetParameter(12)

n = arcpy.GetParameterAsText(13)

o = arcpy.GetParameterAsText(14)

# Create insert cursor for temporary table
rows = arcpy.InsertCursor(outpath + "temptbl","")

#Inserts a row into the table
try:
    for x in xrange(1,2):
        row = rows.newRow()
        row.setValue("GDA_FORM_NUMBER", a)
        row.setValue("JOB_COORDINATOR", b)
        row.setValue("JC_PHONE_NUMBER", c)
        row.setValue("SUPERVISOR", d)
        row.setValue("SR_PHONE_NUMBER", e)
        row.setValue("CONTRACTOR", f)
        row.setValue("PLANT_AREA", g)
        row.setValue("ESTIMATED_START", h)
        row.setValue("ESTIMATED_END", i)
        row.setValue("GDA_DUE_DATE", j)
        row.setValue("CENTER_X_COORD", k)
        row.setValue("CENTER_Y_COORD", l)
        row.setValue("WORKAREA", m)
        row.setValue("CLOSED", n)
        row.setValue("DESCRIPTION", o)
        row.setValue("EXTERNAL_SOURCE", "GDA PERMIT TABLE")
        row.setValue("EXTERNAL_SOURCE_DOCUMENT", "GDA FORM")
        row.setValue("INTERNAL_ROW_CREATED_DATE", time.strftime("%x %X", time.localtime()))
        row.setValue("INTERNAL_ROW_CREATED_BY", userName())
        row.setValue("INTERNAL_ACTIVE_IND","Y")
        rows.insertRow(row)
except:
    print arcpy.GetMessages(1)

# Delete cursor and row objects to remove locks on the data
del row
del rows

0 Kudos
DanPatterson_Retired
MVP Emeritus

Sorry, can't find userName() as a reference property anywhere except when associated with server stuff

0 Kudos
Sheri_AnneSantos
New Contributor II

it's okay Dan. Thanks for trying to help.

0 Kudos
XanderBakker
Esri Esteemed Contributor

If you look at the thread on stack overflow:

portability - Is there a portable way to get the current username in Python? - Stack Overflow

... there are some examples provided to get the current user name which worked on my system (Windows 8.1, Python 2.7):

import os

print os.environ["USERNAME"]

print os.path.split(os.path.expanduser('~'))[-1]

import getpass

print getpass.getuser()

They all printed my current user name.

Kind regards,

Xander

Sheri_AnneSantos
New Contributor II

thanks xander. i used getpass and it worked

0 Kudos