Q:

Python program to delete a file

belongs to collection: Python file handling programs

0

The methods that can be used to perform these tasks are imported in the python program using os library in python.

Deleting a file

Deleting a file will simply delete the given file from its directory. This is done using remove() method present in os library.

Syntax:

os.remove()

The method accepts the path or path of the file to be deleted.

We can delete a file present in your system using remove() method by entering the full path of the file to the function.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program to delete a file in python

# importing os library...
import os

# main method 
def main():
    # removing file using remove method 
    os.remove("data.txt")
    print("File 'data.txt' is deleted!")

if __name__=="__main__":main()

Output:

File 'data.txt' is deleted!

Explanation:

In the above code, we have deleted the file data.txt using the os.remove() method.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Choice-based read-write program in Python - Basic ... >>
<< Python program to read data from file and extract ...