Reboot yourself in Python 3

Overview

I was happy that it went well, so it's a memo ...

background

There was a scene in the python code where I wanted to restart myself with new arguments. I thought about handing it over to another shell or Python, but the code seemed to be confusing, so ...

I referred to the following article. https://stackoverflow.com/questions/31447442/difference-between-os-execl-and-os-execv-in-python

Conclusion


import os
import time
import sys

gets=int(sys.argv[1])
cnt=0
while cnt<5:
    print(cnt+gets)
    cnt+=1
    time.sleep(1)

os.execl(sys.executable, 'python', __file__,str(cnt+gets))

It is important that the second argument is naming.

1st argument → python executable file
Second argument → task name
Third argument → Python file to execute
(The rest is an argument ...)

I was really worried.

Recommended Posts

Reboot yourself in Python 3
Quadtree in Python --2
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Meta-analysis in Python
Unittest in python
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
N-Gram in Python
Programming in python
Plink in Python
Constant in python
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Constant in python
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Quad-tree in Python
Reflection in Python
Chemistry in Python
Hashable in python
DirectLiNGAM in Python
LiNGAM in Python
Flatten in python
flatten in python
Sorted list in Python
Daily AtCoder # 36 in Python
Clustering text in Python
Daily AtCoder # 2 in Python
Implement Enigma in python
Daily AtCoder # 32 in Python
Daily AtCoder # 6 in Python
Daily AtCoder # 18 in Python
Edit fonts in Python
File operations in Python
Read DXF in python
Daily AtCoder # 53 in Python
Key input in Python
Use config.ini in Python
Daily AtCoder # 33 in Python
Solve ABC168D in Python
Logistic distribution in Python