May-05-2020, 09:31 PM
Hi! New to the coding world and developing my first script. I want the script to store a date of last login (previouslogin) and then compare it with today's date (currentlogin) to calculate a delta of days and then perform the action of deleting a folder if the delta is greater than a certain value and if its not then simply store 'currentlogin' date as 'previouslogin' date so that the script can rerun on the next login. Important point is that, 'currentlogin' date has to become 'previouslogin' date for the next instance of running the script. Here is what I have written.
And here is what I am getting:
import datetime import shutil import arrow #an alternative library for handling dates and times # Step 1: Read the previous login date print(previouslogin(currentlogin)) # Step 2: Check current login date today = datetime.date.today() currentlogin = today.strftime("%Y-%m-%d") print(currentlogin) #calculating date delta using Arrow and somehow the delta function in datetime was not working a = arrow.get(previouslogin) b = arrow.get(currentlogin) delta = b-a print (int(delta.days)) # Step 4: If part OR 'decision part' if delta.days > 1: shutil.rmtree("folder2bdel") print("Folder removed!") else: #Step 5: Mark 'currentlogin' as 'previouslogin' def previouslogin(currentlogin): return(previouslogin(currentlogin)) print ("The job is done!") #Step 6: Exit the program (Yet to be written)
And here is what I am getting:
Output:Traceback (most recent call last):
File ".\defautodel.pyw", line 6, in <module>
print(previouslogin(currentlogin))
NameError: name 'previouslogin' is not defined
I am very new and learning it on my own so I may not be able to understand too much technicality at this point of time. Any help to make the script work as intended would be appreciated.