Further to our earlier post on SCN Question Read using Python here, let us proceed to automate the same task on the local machine(Desktop/Laptop) at the end user side for now, later we could explore cloud hosting.
Instead of manually running the task every time, what if we get a regular update of the Questions posted everyday on our machine automatically? Consider a scenario, where you start the day, keeps working on your computer, our particular task run in the background and show a list of items on the desktop for your immediate perusal.
Fine, we proceed in the following manner.
Step 1: Write the Questions to a text file on the local folder of the user.
Step 2: Create a Batch file to execute the task manually(outside of our IDE)
Step 3: Schedule a task(preferably every day at a certain time) to execute the above bat.
Ok, let us get started.
Step 1: Text File
We start modifying our program by first importing the ‘os.path‘ module in Python to handle the file or folder paths we require to store the results, in this case the questions from SAP Community.
With this in hand, we chose a Directory of our choice on the machine(“D:/” in my case. Getting the file on your Desktop would be a nicer idea!) and proceed to create relevant text file there.
Now, if we keep on creating a file with a hard coded name on the folder, every time we run the our program, this particular file would get overwritten. How do we avoid that? Just simply throw in a timestamp from the Python ‘datetime’ class.
This helps us create text file in the particular folder as below:-
Now let us try to pass the data(Questions) to this text file. “w” is for write mode with overwriting and create new if not existing functionalities.
Finally pass our questions to this file.
Close the file in the end.
Throw in a little bit of beautification for better readability.
Step 2: Batch File
Now we proceed to run this application from outside of our IDE. A batch file simply stores the executable commands in a serial order. We create text file and rename it to “.bat” and then run the same(If Windows 10, run with Administrator rights).
It simply contains the command to execute the python file. Assuming that you had already set the path for Python under system variables.
Once done, rename the file with “.bat” extension and keep it in a folder of your choice. I have kept it in the same project folder.
Step 3: Schedule the Task
Now comes the scheduling part of the application. Open the “Task Scheduler” on your (Windows) machine, create new task, browse to your Batch folder above, and provide a time(may be 11 am every day) to run the same.
Browse and provide the path to the above batch file under the task properties.
Result available for us at the folder location specified. The task runs everyday at the specified time(provided the computer is on). For testing, we could either run it from the scheduler or specify a minute from now.
Thank you for reading, updated code available on Git Repo: https://github.com/jakes2255/ScnQuestionRead