Skip to main content
Python 3Scripting

Working with DATE and TIME values in Python

By April 17, 2013September 12th, 2022No Comments

As we build towards the final project application we need to be able to calculate expiry dates. This can be done by adding a number of days or months (or any other form of timing) to the current data and time. For this we need to understand the dates within Python and how we can utilize them to our best advantage. In this video we will also look at the os.system method that allows us to shell out to run programs on the host OS. We shall work at the command line and within scripts with python 3 on our Linux host.

	#!/usr/bin/python3
	import sys, os, crypt
	from datetime import date, timedelta
	now = date.today()
	end = now + timedelta(days=5)
	expire = end.isoformat()
	if not os.geteuid()==0:
		sys.exit("Only root can create users")
	password = "Password1"
	encPassword = crypt.crypt(password,"aa")
	os.system("useradd -m -p "+encPassword+" -e "+expire+" user1")
	print("Done")