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.



filter_none

edit
close

play_arrow

link
brightness_4
code

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 !")

chevron_right


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.

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.