Python With As

In Python you can do the typical way of opening/writing to a file by calling it with: my_var = open(“my file.txt”,”r”) or “w” for write…

In such cases you need to close the file when you’re done with a my_var.close()

Instead you could also use:

with open(“my_file.txt”, “w”) as my_var:
my_var(“Write this to the file.”)

That will auto close it after executing.

Checking if a File is Open or Closed

To check if a file is open or not, you can use the method: closed



Will return a boolean True (meaning closed) or False (meaning open.)

Leave your vote

0 points
Upvote Downvote

Total votes: 0

Upvotes: 0

Upvotes percentage: 0.000000%

Downvotes: 0

Downvotes percentage: 0.000000%

About Admin 329 Articles
I work for a Telecom company writing and testing software. My passion for writing code is expressed through this blog. It's my hope that it gives hope to any and all who are self-taught.