Computer Science, asked by pankajkumar7812, 9 months ago

What will be the output of the following code :
f = open(‘file.txt')
print ( f.closed )
f.close()
print ( f.closed )​

Answers

Answered by rishavsharma21pd1prg
7

Answer:

It will be error because (f.closed) will be treated as identifier in print

Answered by poojan
0

Given code :

f = open('file.txt')

print(f.closed)

f.close()

print(f.closed)

Output 1 : When file exists.

FALSE

TRUE

Output 2 : When file doesn't exist.

FileNotFoundError: [Errno 2] No such file or directory: 'file.txt'

Explanation :

  • In Output 1 (When file exists), f = open('file.txt') opens the file named file.txt.

  • Then, f.closed checks whether the file is in closed mode or not. As it is in open mode, the condition results and prints FALSE.

  • Thereafter, f.close() closes the file present in the object f, which is file.txt

  • Then, f.closed checks if the file is in closed mode or not. As it is closed using f.close() function, the condition prints TRUE, this time.

  • Coming to Output 2 (When the file doesn't exists),  f = open('file.txt') tries to find the file named file.txt and fails as it is not in existence. So, it raises FILENOTFOUNDERROR in the first statement itself.

Learn more :

1) File extension of python is​

https://brainly.in/question/18327723

2) Opening a file in append mode in Python

https://brainly.in/question/5721721

Similar questions