How to make a Python script run on weekdays?

WBOY
Release: 2023-04-12 09:55:21
forward
1369 people have browsed it

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}")
Copy after login

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!

Related labels:
source:51cto.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!