python: Python script knows user that started this script last time?
Is it possible to store name of the user that started my script last? I want to have somethi开发者_C百科ng like this:
- user1 launched script
- user2 launched script
python myscr.py Last started by: user1 ...
Look at getpass.getuser()
>>> import getpass
>>> getpass.getuser()
'me'
EDIT :
Well in general case to log each time the scripts runs and the user i will definitely use logging for that; something like this maybe:
import logging
import getpass
FORMAT = "DATE : %(asctime)-15s USER : %(message)s FILE : %(filename)s"
logging.basicConfig(level=logging.DEBUG,
format=FORMAT,
filename='users.log',
filemode='a')
logging.debug(getpass.getuser())
Ouput :
DATE : 2011-01-11 15:06:26,465 USER : me FILE : zzz.py
Yes, you can store it. In a file, or in the windows registry if you are on windows, or in a database, or in a datastore if you are on some server, or you can store it on a server.
Maybe you can give some more information about your actual problem, and also tell us what you have tried, and why that failed?
I think you could use the getpass module. Use getpass.getuser()
, and then just store that in a file for the next execution?
f = open('lastuser')
f.write(getpass.getuser())
f.close()
# Next time
user = open('lastuser').read()
精彩评论