with
statement PTC-W0010with
statement to open a file105 worksheet.freeze_panes(3, 0)
106
107 fn = ",".join(stations)
108 res = open("/tmp/ss.xlsx", "rb").read()109 os.unlink("/tmp/ss.xlsx")
110 return res, fn
111
with
statement to open a file20 "tmax": row[3],
21 "tmin": row[4],
22 }
23o = open("kbs_century.txt", "w")24
25for year in range(1951, 2013):
26 for col in ["prec", "tmin", "tmax"]:
with
statement to open a file180 df.to_excel(writer, sheet_name="Data", index=False)
181 worksheet = writer.sheets["Data"]
182 worksheet.freeze_panes(3, 0)
183 payload = open("/tmp/ss.xlsx", "rb").read()184 os.unlink("/tmp/ss.xlsx")
185 return [payload]
186
with
statement to open a file109 df.to_excel(writer, sheet_name="Data", index=False)
110 worksheet = writer.sheets["Data"]
111 worksheet.freeze_panes(3, 0)
112 payload = open("/tmp/ss.xlsx", "rb").read()113 os.unlink("/tmp/ss.xlsx")
114 return [payload]
115
with
statement to open a file171 df.to_excel(writer, sheet_name="Data", index=False)
172 worksheet = writer.sheets["Data"]
173 worksheet.freeze_panes(3, 0)
174 payload = open("/tmp/ss.xlsx", "rb").read()175 os.unlink("/tmp/ss.xlsx")
176 return [payload]
177
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.")