with
statement PTC-W0010with
statement to open a file1104 except EnvironmentError:
1105 pass
1106 if not present:
1107 f = open(".gitattributes", "a+")1108 f.write("%s export-subst\n" % versionfile_source)
1109 f.close()
1110 files.append(".gitattributes")
with
statement to open a file1095 files.append(versioneer_file)
1096 present = False
1097 try:
1098 f = open(".gitattributes", "r")1099 for line in f.readlines():
1100 if line.strip().startswith(versionfile_source):
1101 if "export-subst" in line.strip().split()[1:]:
with
statement to open a file 935 # _version.py.
936 keywords = {}
937 try:
938 f = open(versionfile_abs, "r") 939 for line in f.readlines():
940 if line.strip().startswith("git_refnames ="):
941 mo = re.search(r'=\s*"(.*)"', line)
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.")