- To remove or delete the existing file, python provides os commands.
- If we have a file named as 'myfile.txt' and we want to delete this file then,
#create file and insert data
f.write("This is the file")
#read that file
#delete that file
#read file to check file is deleted or not
print("File not exist")
This is the file
File not exist
-In this program, initially we create a file and insert data and read the file, then we delete the file by os.remove('myfile.txt'), then we again try to read that not existing file that's by try and except block are insert so that program will not give any error.
-If we try to delete this file again then-
#here myfile.txt not exist but still we delete then-
FileNotFoundError: [WinError 2] The system cannot find the file specified: 'myfile.txt'