Use the "with" keyword when dealing with file objects
It is good practice to use the "with" keyword when dealing with file objects. This has the advantage that the file is properly closed after its suite finishes, even if an exception is raised on the way. It is also much shorter than writing equivalent try-finally blocks
This commit is contained in:
@@ -96,14 +96,11 @@ class Aireplay(Thread):
|
||||
while self.pid.poll() is None:
|
||||
time.sleep(0.1)
|
||||
if not os.path.exists(self.output_file): continue
|
||||
# Read output file
|
||||
f = open(self.output_file, "r")
|
||||
lines = f.read()
|
||||
f.close()
|
||||
# Clear output file
|
||||
f = open(self.output_file, "w")
|
||||
f.write("")
|
||||
f.close()
|
||||
# Read output file & clear output file
|
||||
with open(self.output_file, "r+") as fid:
|
||||
lines = fid.read()
|
||||
fid.seek(0)
|
||||
fid.truncate()
|
||||
for line in lines.split("\n"):
|
||||
line = line.replace("\r", "").strip()
|
||||
if line == "": continue
|
||||
|
||||
Reference in New Issue
Block a user