Open In App

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:



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.

Article Tags :