Introduction to Python Delete File
One must know that Python (interpreted language) also helps in deleting the file which contains information/an empty file only if you don’t need that particular file in your required directory/folder. Deleting a File using the Python Programming involves using the importing of the OS module and also involves using the “os.remove()” function. Whatever the file format and whatever the information present we can easily delete using the file name/file path inside of the “os.remove()” function but before this you have to import the OS MODULE from the Python Library.
print (“Now the file file_name_1.txt is removed !!!”)
How to Delete Files from Python?
Deleting is the only concept of removing the file/files from a specific required directory when the file is no longer needed for any type of usage in the upcoming syntax of the programming language. In order to know how to delete file/files using the Python needs to know some of the specific steps.
There are Opening Python File window/Opening File which is with the .py extension, Entering the code, Running the Python Program File.
1. Opening Python File Window/.Py Extension File
Opening the Python File/File window is nothing but opening the editor where you are going to type the programming lines of code.
You can also create a new file using the normal text editor and then you can save it with the .py extension at the end of the file name. This is also equal to the opening of the python programming file after creating it using the text editor.
2. Entering the Code
- Code to remove a file using the Python Programming language involves the usage of “os.remove()” function with the appropriate file name or the file path along with the .py extension file inside the “os.remove()” function.
- In Python most of the code seems simple than many other programming languages in the programming world. Python Language usually locates the current directory so if your file which you want to delete can be deleted using “os.remove()” function without specifying the whole file path. So you can easily delete or remove the current directories file without specifying the whole path which starts from the local disk. Check this link to understand better : file:///C://Users/PAVAN/Desktop/test/python/a.py). After this you can specify the print statement in the python programming code after specifying the file remover code/function.
- You can also remove/delete the specific directory using the “os.rmdir()” function. Folder/ Directory will be deleted even if it contains many folders/files and even if the Direcory is empty.
- There is one more functions in python which also helps in deleting or removing the specific directory. It is called “shutil.rmtree()” function. “shutil.rmtree()” function is some what dangerous because it will deletes each and everything which are in the directory without even checking what is present in it. Python just assumes that the programmer/user knows what he/she is doing. Shutil.rmtree() will delete all the sub directories and all the files inside of the directory and the sub directories. So you will be losing the data so easily by using the shutil.rmtree() function.
3. Running the Python Program File
- In order to run the Python Programming Language which contains the file/files remover code, you can open the compiler/command prompt. Then you go to the specific directory of the python file and then we have to run the program using “python filename.py”. Then the command prompt or the python compiler will run the removing code and deletes the file / files from the specific directory.
- If one want to deletes all the files in the specific directory then they can use the os.rmdir() or shutil.rmtree() to delete all the files and folders in the specific directory.
Lets us discuss the Examples:
The below example involves in deleting the specific file from the specific directory. This is done only if we know the path of the file/file’s folder. In the below program I imported the OS module from the python library and then is used os.remove(“pythonfile1.txt”) to remove/delete the file “pythonfile1.txt” from the list of files present in the current directory where the exact file present. Actually the file “pythonfile1.txt” is present but after running the code file “pythonfile1.txt” will be removed from the list of the files in the current directory but we don’t know because we didn’t passed any print statement to know what happened on the command prompt console/ any other python interpreter/compiler. At first list of the directories present are shown in the below image check it.
The above example’s output has the list of the files actually present in our current directory. Check it to know what are the files with different types of available and check the below code and implement it then after getting the output compare the example 1, example 2 output so that you will know what is the difference and what are removed from the list of files which are in the “python files” directory.
In the below example, at first we imported the OS Module then introduced an if and else statements to know whether the file exists or not. If not existed then else statement will be produced without producing error. In the IF statement “pythonfile2.txt” is checked whether it is existed or not then if existed that specific file will be deleted. If the “pythonfile2.txt” is not available in the list of the files of the directory then the python interpreter will proceed to the ELSE statement and prints the statement which is specified. Here “pythonfile2.txt” existed so the file will be removed.
In the second IF statement a text format file is checked just like the 1st IF ELSE statement but here the file “pavan kumar sake.txt” is not present/existed in the directory so the IF condition failed and python interpreter went to run the ELSE condition and prints the statement specified which is in the print() function.
print("File not found to delete !!!")
if os.path.exists("pavan kumar sake.txt"):
os.remove("pavan kumar sake.txt")
print("pavan kumar sake.txt is not found in the directory !!!")
In the below example, os module imported and then a, b variables are created and assigned the filenames which are now we want to delete. Here two deleting functions are used by using two variables and then the print statements are used to know whether the files “pythonfile3.txt” and “pythonfile4.txt” are deleted. Compare the example 2, example 3’s output then you will know what are the files deleted as we needed.
print ("%s is deleted !!"%a)
print ("%s is deleted !!"%b)
I hope you learnt what is meant by Python delete file along with it’s syntax to understand the deleting file concept. Here you can also know how to delete file/files using Python along with some various examples above.
This is Guide to Python delete file along with it’s syntax to understand the deleting file concept with Example. You may also look at the following articles to learn more –