Python Remove Or Delete File
Maybe your like
- Free Android Apps
Learn Python
- Python File Operations
- Files - CRUD Operations
- Python - Create new file
- Python - Read file
- Python - Write to file
- Python - Append to file
- Python - Delete file
- Python - Rename file
- Python - Copy file
- Python - Print to file
- Python - Print from file
- Python - Replace file extension
- Python - Open file in read mode
- Python - Open file in write mode
- Directory - CRUD Operations
- Python – Create a directory
- Python – Remove directory
- Python – Rename directory
- Python – Copy directory
- Python - Get list of files in a directory
- Python - Get list of all files in a directory and its sub-directories recursively
- File Checks
- Python – Check if file exists
- Python – Check if directory exists
- Python – Check if file is writable
- Python – Check if file is readable
- Python – Check if directory is empty
- Python – Check if specified path is file or directory
- File Meta
- Python – Get file size
- Python – Get file extension
- Python – Get file created time
- Python – Get file last modified time
- Python – Get file last access time
- Python – Update file last modified time
- Text File Operations
- Python – Write string to text file
- Python – Write list of strings to text file
- Python – Find unique words in text file
- Python – Append text to file
- Python – Replace a string in text file
- Python – Replace multiple spaces with single space in text file
- Python – Resize or truncate text file to specific size
- Python – Count number of words in a text file
- Python – Count number of characters in text file
- Python – Count occurrences of a word in text file
- Text File Operations
- Python - Read CSV file
- Error Handling
- Handling Python FileNotFoundError
Python - Delete File
To remove or delete a file using Python, call the remove() method of the os library with the path to the file passed as an argument to the function.
In this tutorial, we will learn how to delete a file and explore different scenarios that may occur during the deletion process, such as handling missing files.
Examples
1. Remove a Given File
This example demonstrates how to delete a file named data.txt, located in the same directory as the program.
Python Program
import os os.remove('data.txt') print('The file is removed.')Explanation (Step-by-Step):
- Import the os module, which provides functions to interact with the file system.
- Call os.remove('data.txt') to delete the file named data.txt from the current working directory.
- If the file exists, it will be removed, and the program will continue to execute the next line.
- Print the message 'The file is removed.' to indicate successful deletion.
If the file is located in a different directory, specify the absolute path to the file:
Python Program
import os os.remove('C:\workspace\python\data.txt') print('The file is removed.')
Explanation (Step-by-Step):
- Import the os module.
- Use os.remove() with the absolute file path 'C:\workspace\python\data.txt' to delete the file located in a specific directory.
- If the file exists at the specified path, it will be deleted.
- Print the message 'The file is removed.' to confirm the operation.
2. Try to Remove a File That Does Not Exist
This example shows the error raised when attempting to delete a file that does not exist at the specified path.
Python Program
import os os.remove('C:\workspace\python\aslkdjfa.txt') print('The file is removed.')Output
FileNotFoundError: [WinError 2] The system cannot find the file specified: 'aslkdjfa.txt'Explanation (Step-by-Step):
- Import the os module.
- Attempt to delete a file named 'aslkdjfa.txt' located at the path 'C:\workspace\python\' using os.remove().
- Since the file does not exist, Python raises a FileNotFoundError.
- The error includes details such as the error code [WinError 2] and the path of the missing file.
- To avoid this error, check if the file exists using os.path.exists() before calling os.remove().
Summary
In this tutorial, we explored how to delete a file using the os library's remove() function in Python, including scenarios where the file exists or does not exist.
Python Libraries
PythondatetimePythonflaskPythonjsonPythonloggingPythonmathPythonmysqlPythonMatplotlibPythonnltkPythonnumpyPythonopencvPythonpandasPythonphonenumbersPythonpicklePythonpillowPythonpymongoPythonrandomPythonrequestsPythonseleniumPythonsqlite3PythontkinterTag » How To Delete File In Python
-
Python Delete File - W3Schools
-
How Do I Delete A File Or Folder In Python? - Stack Overflow
-
How To Delete File In Python - Scaler Topics
-
How To Delete A File In Python
-
Python Delete File: A Step-By-Step Guide | Career Karma
-
How To Create And Delete A File In Python? - ProjectPro
-
Delete A Directory Or File Using Python - GeeksforGeeks
-
Delete (Remove) Files And Directories In Python - PYnative
-
Python Remove File - How To Delete A File? | Flexiple Tutorials
-
Python Delete A File 🗑️ - YouTube
-
How To Delete A File Or Folder Using Python - Data To Fish
-
Python Delete File [Ultimate Guide] - Finxter
-
Python File Operations - Read And Write To Files With Python
-
How To Delete A File In Python – With Example - CodeBerry