+ 1
Python and "with" statement
I have recently seen a "with" statement used like so: with open("file.txt","r") as f: f.read() As much as i understand it... this shortcuts come part of method or variable. So in my thinking way this should work: list = [1,2,3] with list as l: print(l[1]) But it gives me error... So what is "with" statement used for?
4 odpowiedzi
+ 2
with - specifically for OS BIOS it is used opening files
with open('txt.js','r') as f:
# with helps name the file to a variable f
lines = f.readlines ()
if lines == None:
pass
else:
print(lines)
+ 2
and calling a variable list is forbidden as it is a reserved word :)
+ 1
So i'm unable to get variables or methods like that but in shorter form?
+ 1
Thanks :)