diff mbox series

[08/17] rt-tests: hwlatdetect: Use "with" for opening files

Message ID 20211111204207.10167-8-jkacur@redhat.com
State New
Headers show
Series [01/17] rt-tests:hwlatdetect: Remove useless object in class declaration | expand

Commit Message

John Kacur Nov. 11, 2021, 8:41 p.m. UTC
Use "with" for opening files

Signed-off-by: John Kacur <jkacur@redhat.com>
---
 src/hwlatdetect/hwlatdetect.py | 43 ++++++++++++++++------------------
 1 file changed, 20 insertions(+), 23 deletions(-)
diff mbox series

Patch

diff --git a/src/hwlatdetect/hwlatdetect.py b/src/hwlatdetect/hwlatdetect.py
index 2fabbb55b242..a530867777a7 100755
--- a/src/hwlatdetect/hwlatdetect.py
+++ b/src/hwlatdetect/hwlatdetect.py
@@ -39,14 +39,13 @@  class DebugFS:
         self.premounted = False
         self.mounted = False
         self.mountpoint = ''
-        f = open('/proc/mounts')
-        for l in f:
-            field = l.split()
-            if field[2] == "debugfs":
-                self.premounted = True
-                self.mountpoint = field[1]
-                break
-        f.close()
+        with open('/proc/mounts') as f:
+            for l in f:
+                field = l.split()
+                if field[2] == "debugfs":
+                    self.premounted = True
+                    self.mountpoint = field[1]
+                    break
 
     def mount(self, path='/sys/kernel/debug'):
         if self.premounted or self.mounted:
@@ -74,9 +73,8 @@  class DebugFS:
     def getval(self, item, nonblocking=False):
         path = os.path.join(self.mountpoint, item)
         if nonblocking == False:
-            f = open(path)
-            val = f.readline()
-            f.close()
+            with open(path) as f:
+                val = f.readline()
         else:
             f = os.fdopen(os.open(path, os.O_RDONLY|os.O_NONBLOCK), "r")
             try:
@@ -97,10 +95,9 @@  class DebugFS:
 
     def putval(self, item, value):
         path = os.path.join(self.mountpoint, item)
-        f = open(path, "w")
-        f.write(str(value))
-        f.flush()
-        f.close()
+        with open(path, "w") as f:
+            f.write(str(value))
+            f.flush()
 
     def getpath(self, item):
         return os.path.join(self.mountpoint, item)
@@ -378,10 +375,10 @@  class Tracer(Detector):
 
     def save(self, output=None):
         if output:
-            f = open(output, "w")
-            for s in self.samples:
-                f.write("%s\n" % str(s))
-            print("report saved to %s (%d samples)" % (output, len(self.samples)))
+            with open(output, "w") as f:
+                for s in self.samples:
+                    f.write("%s\n" % str(s))
+                print("report saved to %s (%d samples)" % (output, len(self.samples)))
 
     def display(self):
         for s in self.samples:
@@ -443,10 +440,10 @@  class Hwlat(Detector):
 
     def save(self, output=None):
         if output:
-            f = open(output, "w")
-            for s in self.samples:
-                f.write("%s\n" % str(s))
-            print("report saved to %s (%d samples)" % (output, len(self.samples)))
+            with open(output, "w") as f:
+                for s in self.samples:
+                    f.write("%s\n" % str(s))
+                print("report saved to %s (%d samples)" % (output, len(self.samples)))
 
     def cleanup(self):
         if not self.kmod.unload():