with
statement PTC-W0010with
statement to open a file 19
20def convert(file_name):
21 # Open csv file
22 csv = io.open(file_name + ".csv", mode="w", encoding="utf-8") 23
24# Convert file_name.tsv to file_name.csv
25 with io.open(file_name + ".tsv", mode="r", encoding="windows-1252") as tsv:
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.")