with
statement PTC-W0010with
statement to open a file150
151 if self.config["natural_language_processing"]["analyze_sentiment"]:
152 # Load text from file
153 text = open(154 self.config["natural_language_processing"]["text_path"]
155 ).read()
156
with
statement to open a file131 while True:
132 if self.config["natural_language_processing"]["recognize_entities"]:
133 # Load text from file
134 text = open(135 self.config["natural_language_processing"]["text_path"]
136 ).read()
137
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.
f = open('/tmp/.deepsource.toml', 'w')
f.write("config file.")
# No `f.close()` statement: file may remain unaccessible
with open('/tmp/.deepsource.toml', 'w') as f:
f.write("config file.")