Always execute code and end python script

Is there a way in Python to always execute a block of code at the end of a program (ban kill -9 ?

We have a Jenkins project that runs a python script as part of the build process. If the developer decides to interrupt the work, then there are many artifacts left that can (and can) affect future builds.

In any case, to make sure that the python script part is being cleaned up?

+8
python jenkins
source share
1 answer

Use atexit output handlers. Here is an example from python docs :

 try: _count = int(open("counter").read()) except IOError: _count = 0 def incrcounter(n): global _count _count = _count + n def savecounter(): open("counter", "w").write("%d" % _count) import atexit atexit.register(savecounter) 
+11
source share

All Articles