Ever wanted to delete a file, that too from a Python program, with a few lines of code? Well, you are in the right place. Today we are going to learn how we are going to be deleting a file by using Python.
Note: We will be importing the os library and going to use the os.remove() function to remove the desired file. If you don’t have os library, then open Command Prompt and write pip install os, to install the required os library.
Below is the Python implementation –
The desired file to be deleted:
Sample run of the program
When we enter the name of the file to be deleted:
- Delete a directory or file using Python
- How to delete data from file in Python
- Python program to reverse the content of a file and store it in another file
- Python program to copy odd lines of one file to other
- Python Program to merge two files into a third file
- Python program to modify the content of a Binary File
- Python program to extract Email-id from URL text file
- Python program to Reverse a single line of a text file
- Python program to Count the Number of occurrences of a key-value pair in a text file
- reStructuredText | .rst file to HTML file using Python for Documentations
- Python program to read character by character from a file
- Python program to read file word by word
- numpy.delete() in Python
- Python | Delete elements in range
- Python | Pandas Index.delete()
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.