75661

Python to run a piece of code at a defined time every day

Question:

In my python program, I would like it to run a piece of code at a pre-defined time every weekday, let say 2pm Mon - Fri.

How may I do it please?

Answer1:

You can use "schedule" library

to install, on terminal enter:

pip install schedule

here is an example of the code you want:

#!/usr/bin/python import schedule import time def job(): print("I am doing this job!") schedule.every().monday.at("14:00").do(job) schedule.every().tuesday.at("14:00").do(job) schedule.every().wednesday.at("14:00").do(job) schedule.every().thursday.at("14:00").do(job) schedule.every().friday.at("14:00").do(job) while True: schedule.run_pending() time.sleep(1)

or you can read the documents to see the other functions <a href="https://schedule.readthedocs.io/en/stable/" rel="nofollow">Click Here</a>

good luck!

Recommend

  • OpenCV : algorithm for simple image rotation and reduction
  • How to use correct regional format (date & time) of Windows Phone 8.1
  • How to use MonkeyDevice.instrument?
  • Content-by-user: two tables with columns of the same name or join?
  • C++ Loadlibrary() error 3765269347
  • Parsing user time input in Java/GWT
  • hibernate sessionfactory as a global jndi resource
  • Incomplete data: Delta source ended unexpectedly while git svn fetch
  • Can't install scipy
  • shell script for grabbing data and subtracting
  • Algorithm for placing nodes on a circle considering their distance to eachother
  • ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS in not firing
  • Round brackets not showing up correctly in RightToLeft flow direction in WPF
  • EJB stateless - Private members initialisation
  • RavenDB OrderByDescending and Take - Incorrect Results
  • Magento invalid cron expression
  • How to auto update a record in database?
  • How to retrieve Business Component rows in SIebel Open UI PM?
  • Where can in find the locale objects for d3.js for different countries
  • RewriteCond and rewriteRule to redirect depending on the domain
  • working with bool value from View in javascript
  • Failed to execute goal net.alchim31.maven:scala-maven-plugin:3.2.2
  • Error while installing ggplot in python
  • How to install PyAutoGUI
  • Change device language on Android 6.0 (Android M)
  • Interpreting STRACE output - pipes and forks
  • Scanner nextInt() and hasNextInt() problems
  • MeeGo Handset Emulator not starting on Windows 7
  • Error in installing package: fatal error: stdlib.h: no such file or directory
  • jQuery: How to AJAXify WordPress Search?
  • OSX - always hide certain files
  • Compiling dlib on OS X
  • Trying to get the char code of ENTER key
  • cygwin cannot exec 'git-add--interactive' permission denied
  • How to generate and display a QR Code in ionic 2
  • Is playing sound in Javascript performance heavy?
  • error importing numpy
  • Make VS2015 use angular-cli ng at build time in a .NET project
  • Setting up SourceTree to merge unity3d scenes with UnityYAMLMerge
  • Javascript convert timezone issue