my senf to ralfs senf to tuerd
authorConstantin <constantin@exxxtremesys.lu>
Sun, 15 Sep 2013 22:47:58 +0000 (00:47 +0200)
committerConstantin <constantin@exxxtremesys.lu>
Sun, 15 Sep 2013 22:47:58 +0000 (00:47 +0200)
as suggested in http://stackoverflow.com/questions/10840533/most-pythonic-way-to-delete-a-file-which-may-not-exist

tuerd

diff --git a/tuerd b/tuerd
index 00b2420aade8493b21940f9d14ef2e0735e38d7b..5884d131e1b0d26b0bd67a9f107879a1d907087b 100755 (executable)
--- a/tuerd
+++ b/tuerd
@@ -1,5 +1,5 @@
 #!/usr/bin/python3
-import time, socket, os, stat, atexit
+import time, socket, os, stat, atexit, errno
 from datetime import datetime
 import RPi.GPIO as GPIO
 GPIO.setmode(GPIO.BOARD)
@@ -25,8 +25,12 @@ def doNothing (conn):
 
 # delete a file, don't care if it did not exist in the first place
 def forcerm(name):
-       if os.path.exists(name):
+       try:
                os.unlink (name)
+       except OSError as e:
+               # only ignore error if it was "file didn't exist"
+               if e.errno != errno.ENOENT:
+                       raise
 
 # commands: on a pin do a series of timed on/off switches
 class Pinoutput: