Step-by-Step Execution:
Opening the File (open("test.txt", "w")):
The open() function is used to open the file "test.txt" in write mode ("w").
If the file "test.txt" doesn't exist, it will be created.
If the file already exists, its content will be overwritten.
Writing to the File (file.write("Hello!")):
The write() method writes "Hello!" to the file.
This does not automatically close the file.
Closing the File (file.close()):
The close() method closes the file.
Closing a file releases system resources and ensures the data is properly saved.
Checking if the File is Closed (print(file.closed))
file.closed is a boolean attribute that returns True if the file is closed, False otherwise.
Since we explicitly closed the file with file.close(), file.closed will return True.
Output:
True
This confirms that the file is successfully closed.
0 Comments:
Post a Comment