In: Computer Science
What does each line of this python code do?
import datetime
import getpass
print("\n\nFinished execution at ", datetime.datetime.now())
print(getpass.getuser())
In Python date and time are not inbuilt datatypes on their own
but a module named datetime
can be
imported to work with the date as well as time.
1)import datetime----->>>Here we are importing a package to our python which consists of date and time.Its a combination of date and time along with the attributes year, month, day, hour, minute, second, microsecond, and tzinfo.
2)import getpass---->>>It is a portable password input.By default the keys entered by user in the terminal are not echoed. Also the default prompt that appears on terminal is ‘password’ which can be customized by providing a string as parameter.
3)print("\n\nFinished execution at ", datetime.datetime.now())------>>>It helps us get the current date and time,since we are using inside the print.Hence the date and time gets printed.
4)print(getpass.getuser())------->>>As I previously mentioned that the getpass() function prints a prompt then reads input from the user until they press return and with the combination of getpass and getuser,Sometimes we need to know the login name which we are using to run the script. This is achieved by using the getuser() function.
Example::
import getpass user = getpass.getuser() while True: psd = getpass.getpass("User Name : ",user) if psd == 'Jack': print("You are loggedin") else: print("The password entered by you is wrong.")
output::
User Name: user1 You are loggedin !