Use the
with
statement to open a file108 return self.unique(found)
109
110 def extract(self, name, matches):
111 output = open(self.output, "a+")112 if matches:
113 stdout = ("[%s]" % (name))
114 self.writeln("\n" + stdout, clr.OKGREEN)
Use the
with
statement to open a file 5import os
6
7def header():
8 VERSION = open(os.path.dirname(os.path.realpath(__file__)) + "/VERSION", "r").read() 9 return (" _ ____ _ ___ _ \n / \\ | _ \\| |/ / | ___ __ _| | _____ \n / _ \\ | |_) | ' /| | / _ \\/ _` | |/ / __|\n / ___ \\| __/| . \\| |__| __/ (_| | <\\__ \\\n /_/ \\_\\_| |_|\\_\\_____\\___|\\__,_|_|\\_\\___/\n {}\n --\n Scanning APK file for URIs, endpoints & secrets\n (c) 2020-2021, dwisiswant0\n".format(VERSION))
10
11def argument():
Description
Opening a file using with
statement is preferred as function open
implements the context manager protocol that releases the resource when it is outside of the with
block. Not doing so requires you to manually release the resource.
Bad practice
f = open('/tmp/.deepsource.toml', 'w')
f.write("config file.")
# No `f.close()` statement: file may remain unaccessible
Preferred:
with open('/tmp/.deepsource.toml', 'w') as f:
f.write("config file.")