Opening and working with files in python using one variable

29 Views Asked by At

Let's say I have this code:

s = open('say.txt').read()
print(s.count('A'))

Is it correct to work with the file in this way? I understand that the variable s includes a string from the file. But what happens to the file? Does it close after assigning a value to a variable, or will it be open until the end of the program? And if so, how should it be closed?

I tried to close the file using the close() method, but the executor returned an error.

0

There are 0 best solutions below