通过将当前日期附加到文件中,您可以比较日历日的差异。
代码:
import datetime
new_day = int(datetime.datetime.now().strftime("%d"))
last_day = 0
with open("last_time.txt", "r") as f:
lines = f.read().splitlines()
last_day = lines[-1]
if new_day == int(last_day):
print("Please wait until tomorrow before entering a new value")
with open("last_day.txt", "a+") as f:
last_day = datetime.datetime.now().strftime("%d")
f.write(last_day)
解释:
1. Create a text file(last_time.txt) and give the default day as when you are running the script for first time(as today(24))
2. get the new_day as day from datetime.now() and convert into an integer.
3. By default keeping the last_day=0 or you can give the current date for first time
4. Reading the last time from the last line of the file
5. If new_day and last_day are equal, then print the message to the user.
6. Fetch the current day as last_day and write to a file in append mode.
文件 O/p:
23
24
输出/输出:
Please wait until tomorrow before entering a new value