with
statement PTC-W0010with
statement to open a file231 assert os.path.isdir("./docs")
232 for fname in required_files:
233 assert os.path.exists(fname), fname
234 assert "run: make" not in open(".github/workflows/test.yml").read()235
with
statement to open a file 63def release(c):
64 "Tag the current version as a release on Github."
65 assert "working tree clean" in c.run("git status").stdout
66 version = open("VERSION").read().strip() 67 os.system(f"hub release create {version}") # interactive
68
69
with
statement to open a file 38def newversion(c):
39 "Increment the version number."
40 assert "working tree clean" in c.run("git status").stdout
41 text = open("setup.py").read() 42 version = re.search('version *= *"([0-9.]+)"', text).group(1)
43 print("old version", version)
44 text = re.sub(
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.")