How to enter a Python console script at different parts of it? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to enter a Python console script at different parts of it?

I would like to know how to enter a Python script at different parts of it. E.g. if the script consists of 10 console commands run after one another and I close my terminal after 5 (or manually stop there), I would like to be able to start the script from the 6th command. If I closed it after the 7th command, I would like to start with the 8th command and so on. So, one way would be to have a number Parameter by executing the script that tells where to start. But how do I include it? Just hard-coding if Parameter < x over each and every step to start there seems a bit odd.... However, I guess it would look like that: if Param < 2 Do step 1 If Param < 3 Do step 2 If Param < 4 Do step 3 ... But I guess there must be a better solution to this. Does anyone have any idea on it? :) Edit: I don't want to catch the moment I left the terminal, I could find that out otherwise. I just want to be able to start my script from that/any point. E.g. with "python3 my_script --step 4" -> start at step 4.

18th Aug 2021, 2:32 PM
AngelBuffyFan
AngelBuffyFan - avatar
2 Answers
+ 1
Program starts from begining with every re-execution, so only way to catch the moment “you left” last time, is write step counter on disk, and include reading this parameter on launching. Basicaly you can firstly check, if the file with specific name exists in your working directory, if not - create it and start from very begining. If it exists - read counting variable, that will lead to needed step of your code. Need knoweledge in some basics and maybe intermediate course if Python, https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files
18th Aug 2021, 2:37 PM
Лев Павлов
Лев Павлов - avatar
0
Code to pass parameter of step, to begin script from (not works on SL, it is for use with OS command line): https://code.sololearn.com/cB79m4TtlKv2/?ref=app
22nd Aug 2021, 1:12 AM
Лев Павлов
Лев Павлов - avatar