Home  >  Article  >  Backend Development  >  How to make a Python script run on weekdays?

How to make a Python script run on weekdays?

WBOY
WBOYforward
2023-04-12 09:55:211392browse

Hello, I am Brother Zheng. I recently wrote a script for daily sign-in and answering questions in Python. I want it to run every day during working days, so I found this third-party library-chinesecalendar.

chinesecalendar can be used to determine whether a certain day of a certain month of a certain year is a working day/holiday. Supports 2004 to 2022, including the Spring Festival extension in 2020.

I think there is little value in supporting previous years. After all, they have passed. Fortunately, he supports 2022.

After seeing how it is implemented, it is written down according to the country’s legal holidays:

How to make a Python script run on weekdays?

After all, there is no way to do it. Who will be given time off on which day? It cannot be predicted in advance. We would like to thank LKI[1] for his hard work.

Next, share the simple usage of chinesecalendar:

First install pip install chinesecalendar, and then you can judge:

from datetime import datetime, timedelta
from chinese_calendar import is_holiday, is_workday

day = datetime.now()
week_list = ["一", "二", "三", "四", "五", "六", '日']
for i in range(31):
x = day + timedelta(days=i)
desc = "工作日" if is_workday(x) else "节假日" if is_holiday(x) else ""
print(f"{x.strftime('%Y-%m-%d')} 星期{week_list[x.weekday()]} {desc}")

The running results are as follows:

How to make a Python script run on weekdays?

If you want to use other languages, you can use this Python script [2] to export the constant file.

The above is the detailed content of How to make a Python script run on weekdays?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:51cto.com. If there is any infringement, please contact admin@php.cn delete