Python Forum
Store Previous date to calculate delta from today
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Store Previous date to calculate delta from today
#1
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.

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.
Reply


Messages In This Thread
Store Previous date to calculate delta from today - by Captain_Wolf - May-05-2020, 09:31 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Compare current date on calendar with date format file name Fioravanti 1 318 Mar-26-2024, 08:23 AM
Last Post: Pedroski55
  Python date format changes to date & time 1418 4 724 Jan-20-2024, 04:45 AM
Last Post: 1418
  i tried to install python for the first time today and pretty certain im being remote brianlj 2 603 Oct-03-2023, 11:15 AM
Last Post: snippsat
  Calculate next rows based on previous values of array divon 0 1,847 Nov-23-2021, 04:44 AM
Last Post: divon
  Date format and past date check function Turtle 5 4,422 Oct-22-2021, 09:45 PM
Last Post: deanhystad
  Datetime strp\delta problems korenron 2 2,133 Jun-13-2021, 10:35 AM
Last Post: korenron
  Need to identify only files created today. tester_V 5 4,754 Feb-18-2021, 06:32 AM
Last Post: tester_V
  How to add previous date infront of every unique customer id's invoice date ur_enegmatic 1 2,285 Feb-06-2021, 10:48 PM
Last Post: eddywinch82
  Make list of dates between today back to n days Mekala 3 2,431 Oct-03-2020, 01:01 PM
Last Post: ibreeden
  How to add date and years(integer) to get a date NG0824 4 2,938 Sep-03-2020, 02:25 PM
Last Post: NG0824

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020