with
statement PTC-W0010with
statement to open a file1903 await self.respond(ctx.ctx, embed=self.queue_embed(data[args[0]], page, loc["messages"]["playlist_page_title"].format(args[0]), "", "id"))
1904 if not os.path.isdir("./music"):
1905 os.mkdir("./music")
1906 open(file, "w", encoding="utf-8").write(json.dumps(data))1907
1908 @bridge.bridge_command(name="musicversion", description=loc["descriptions"]["musicversion"])
1909 async def musicversion(self, ctx):
with
statement to open a file1763 async def playlist_func(self, ctx, *, args=None):
1764 file = f"./music/playlist_{ctx.author.id}.json"
1765 if os.path.isfile(file):
1766 data = json.loads(open(file, "r", encoding="utf-8").read())1767 else:
1768 data = {}
1769 if args is None:
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.")