Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




PYTHON - DELETING FILES


To delete a File, we need to import the 'os' module.


'os' module can be said to be a short form of Operating System. It has methods that supports operations that is used to interact with Operating System.


We can use the 'remove( )' method of 'os' module to delete a file.


Example :



import os
os.remove("myfirstfile.txt")


And, the file 'myfirstfile.txt' is deleted from the current directory.


How can we delete a Folder ?


The 'os' module also provides a method named 'rmdir' that is used to delete a folder and its contents.


Example :



import os
os.remove("myfolder")


Let us say we had a folder named 'myfolder' in the current directory. And the 'remove( )' method of 'os' module is used to delete that folder and its contents.


os.remove("myfolder")