How to delete a file using Python

Configurare noua (How To)

Situatie

To delete a file, you must import the OS module, and run its os.remove() function:

Solutie

Pasi de urmat

 

Remove the file “demofile.txt”:

import os
os.remove(“demofile.txt”)

 

To avoid getting an error, you might want to check if the file exists before you try to delete it:

Check if file exists, then delete it:

import os
if os.path.exists(“demofile.txt”):
os.remove(“demofile.txt”)
else:
print(“The file does not exist”)

Tip solutie

Permanent

Voteaza

(1 din 7 persoane apreciaza acest articol)

Despre Autor

Leave A Comment?