Friday, November 15, 2024
Google search engine
HomeLanguagesSchedule a Python Script on PythonAnywhere

Schedule a Python Script on PythonAnywhere

Keeping the computer on 24/7 is not practical, so if you want to execute a Python script at a particular time every day, you probably need a computer that is ON all the time. To make this possible, a website PythonAnywhere gives you access to such a 24/7 computer. You can upload a Python script and schedule it to run at a certain time every day. This availability can be useful, for example, when you want to extract some values (e.g., weather data) from a website and generate a text file with the value or other reports every day.

To schedule, a Python script for execution on PythonAnywhere, follow these simple steps:

  • Sign up on here the account for Beginners is free with some T&C.
  • Go to your Dashboard, Files, Upload a File and upload the Python file you want to schedule for execution.
  • Go to Tasks and set the time of the day you want your script to be executed and type in the name of the Python file you uploaded (e.g., myfirstpyscript.py).
    Note: The time entered should be in UTC.
  • Click “create” and you are done.

The Python file will now be executed every day at your specified time.

Example: Below is a very simple Python script you can use to schedule for execution.




from datetime import datetime
  
  
# Saves a .txt file with file name
# as 2020-01-11-10-20-23.txt
with open(datetime.now().strftime("%Y-%m-%d-%H-%M-%S"), "w")as myfile:
      
    # Content of the file
    myfile.write("Hello World !")


The above code creates a text file and writes the string “Hello World!” in that text file. The name of the text file will be the current date and time. For example one file name example would be 2020-01-11-10-20-23.txt.

That name is generated by datetime.now() indicating the date and time the script was executed. Every time the script is executed, the script generates a new text file with a different name. You will have a new text file created every day.

RELATED ARTICLES

Most Popular

Recent Comments