banner



how to delete a file in python

Python Delete File

Introduction to Python Delete File

One must know that Python (interpreted language) also helps deleting the file containing information/an empty file only if you don't need that particular file in your required directory/folder. Deleting a File using Python Programming involves using the OS module's importing and 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.

Syntax:

Import os
os.remove("file_name_1.txt")
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 specifically required directory when the file is no longer needed for any type of usage in the programming language's upcoming syntax. In order to know how to delete file/files using the Python needs to know some of the specific steps.

Opening Python File window/Opening File 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 python programming file's opening 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 the "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 the "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 python programming code's print statement 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 are one more functions in python, which also helps in deleting or removing the specific directory. It is called the "shutil.rmtree()" function. "shutil.rmtree()" function is some what dangerous because it will deletes each and everything which is 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 python file's specific directory, and then we have to run the program using "python filename.py". The command prompt or the python compiler will then run the removing code and delete the file / files from the specific directory.
  • If one wants to delete all the files in the specific directory, they can use the os.rmdir() or shutil.rmtree() to delete all the files and folders in the specified directory.

Examples

Lets us discuss the Examples:

Example #1

The below example involves in deleting the specific file from the specified 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 the first list of the directories present are shown in the below image; check it.

python delete 1

Syntax:

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

Output:

python delete 2

Example #2

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 the difference is 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 an error. In the IF statement, "pythonfile2.txt" is checked whether it exists or not; if it 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 the python interpreter went to run the ELSE condition and prints the statement specified which is in print () function.

Syntax:

import os
if os.path.exists("pythonfile2.txt"):
os.remove("pythonfile2.txt")
else:
print("File not found to delete !!!")
if os.path.exists("pavan kumar sake.txt"):
os.remove("pavan kumar sake.txt")
else:
print("pavan kumar sake.txt is not found in the directory !!!")

Output:

python delete 3

Example #3

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 example 2, example 3's output; then you will know what the files deleted as we needed are.

Syntax:

import os
a="pythonfile3.txt"
b="pythonfile4.txt"
os.remove(a)
os.remove(b)
print ("%s is deleted !!"%a)
print ("%s is deleted !!"%b)

Output:

a, b variables are created

Conclusion

I hope you learnt what is meant by Python delete file along with its 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.

Recommended Articles

This is a Guide to Python delete file along with its syntax to understand the deleting file concept with Example. You may also look at the following articles to learn more –

  1. Python Class Attributes
  2. Python List count
  3. Python Dictionary Keys
  4. Python User Input

how to delete a file in python

Source: https://www.educba.com/python-delete-file/

Posted by: pennystoing.blogspot.com

0 Response to "how to delete a file in python"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel