Python - Delete File
[Pages:3]Python ? Delete File
Delete File in Python
To delete a file in Python, you can import os package and use remove() function of the os package. In this tutorial, we will learn how to delete a file using Python programming. First we shall look into the syntax of os.remove() function. Secondly, we shall look into examples, where we actually delete a file. The examples cover scenarios: delete file without checking if it is actually present; delete file with a prior check if the file is present.
Syntax
The syntax of os.remove() function is: os.remove(file_path)
os.remove() deletes the file present at the location provided by String value file_path . os.remove() deletes the file if it is present. If the file is not present, nothing happens, not even an error occurs. The execution continues with subsequent program statements. So, it is recommended that you check if the file is present, if you would like to be sure if you have actually deleted the file.
Example 1 ? Delete File in Python
In the following Python program, we are deleting a file present at the location D:/data.txt . example.py import os #delete the file os.remove("D:/data.txt") print('The file is deleted.')
Output The file is deleted.
Example 2 ? Delete File in Python if Present
You can make a check if file is present, prior to the deletion and make sure, if you have actually deleted the file. To check if the file is present, use os.path.isfile() function. os.path.isfile() function returns true if file is present
in the storage. Else the function returns false.
Once we are sure that the file is present, then we can call the function os.remove() to delete the file.
example.py
import os file_path = "D:/data.txt" #check if file is present if(os.path.isfile(file_path)):
#delete the file os.remove(file_path) print('The file is deleted.') else: print('The file is not present.')
Output
If the file is present and deleted, you would get the following output.
The file is deleted.
If the file is not present, and you try to delete, you get the following output.
The file is not present.
Conclusion
In this Python Tutorial, we learned how to delete a file programmatically using Python.
Python Programming
Python Tutorial Install Python Install Anaconda Python Python HelloWorld Program Python Variables Python Variable Data Type Conversion Python Comments
Control Statements
Python If Python If Else Python While Loop Python For Loop
Python String
Python String Methods Python String Length Python String Replace Python Split String Python Count Occurrences of Sub-String Python Sort List of Strings
Functions
Python Functions
Python Collections
Python List Python Dictionary
Advanced
Python Multithreading
Useful Resources
Python Interview Questions
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- installing gdal for python on windows utah state university
- assuring your directory exists sesug
- tm1py documentation read the docs
- pysftp documentation read the docs
- object exist a macro to check if a specified object exists
- project 6 file system princeton university
- python tutorial netcdf national oceanic and atmospheric administration
- the file system poco c libraries
- exif documentation read the docs
- python delete file
Related searches
- delete file in python
- python delete file if exists
- python delete file if it exists
- delete file windows 10 command
- windows 10 delete file association
- delete file association windows 10
- delete file associations settings windows 10
- delete file association
- delete file association win 10
- delete file type association
- how to delete file association
- delete file type associations