The fun(filename) function creates a new file passed to it as a parameter, adds some text and then returns its size. When print(os.path.getsize(filename) is used within the 'with' block it doesn't work as intended.
import os def fun(filename): with open(filename,'w') as file: file.write("hello") print(os.path.getsize(filename)) fun("newFile2.txt") It outputs 0. But when print(os.path.getsize(filename) is used outside the 'with' block as
import os def fun(filename): with open(filename,'w') as file: file.write("hello") print(os.path.getsize(filename)) fun("newFile2.txt") Then it prints the correct output: 5. What's going on behind the scenes?
withblock.