Delete a file with Python

Configurare noua (How To)

Situatie

To delete a file with this script, we can use the os module.It’s recommended to check with a conditional if the file exist before calling the remove() function from the module:

import os

if os.path.exists("<file_path>"):
  os.remove("<file_path>")
else:
  <code>

Backup

Example:

import os

if os.path.exists("famous_actors.txt"):
  os.remove("famous_actors.txt")
else:
  print("This file doesn't exist")

Solutie

Tip solutie

Permanent

Voteaza

(7 din 13 persoane apreciaza acest articol)

Despre Autor

Leave A Comment?