[GRASS-user] Definintion of cleanup in pyhton script

Glynn Clements glynn at gclements.plus.com
Thu Mar 1 11:46:25 EST 2012


Johannes Radinger wrote:

> maybe someone can help me with the cleanup process in a
> GRASS python script I am writing. The script uses the 
> gparser functionality to get the needed files etc via
> the GUI.
> 
> I also try to include a proper cleanup process within
> the script which deletes all the tmp maps created. This
> should also work if the script stops (e.g due to an error).
> So I think I FIRST need to define all the files which
> should be removed. The problem is that my script includes
> a while loop (iteration) so I can't really say which
> files will be created in advance. How can I define a proper
> cleanup in such cases?

Define a list of map names which starts out empty and has names
appended to it as the names are generated.

E.g.:

	tmp_rast = []
	
	def cleanup():
	    for rast in tmp_rast:
	        grass.run_command("g.remove", 
	                          rast = rast,
	                          quiet = True)
	
	def main():
	    ...
	    while ...:
	        next_rast = ...
	        tmp_rast.append(next_rast)
	        ...

	if __name__ == "__main__":
	    options, flags = grass.parser()
	    atexit.register(cleanup)
	    sys.exit(main())

-- 
Glynn Clements <glynn at gclements.plus.com>


More information about the grass-user mailing list