How to delete a file or folder?

前端 未结 13 1269
青春惊慌失措
青春惊慌失措 2020-11-22 12:29

How do I delete a file or folder in Python?

13条回答
  •  难免孤独
    2020-11-22 13:08

    • os.remove() removes a file.

    • os.rmdir() removes an empty directory.

    • shutil.rmtree() deletes a directory and all its contents.


    Path objects from the Python 3.4+ pathlib module also expose these instance methods:

    • pathlib.Path.unlink() removes a file or symbolic link.

    • pathlib.Path.rmdir() removes an empty directory.

提交回复
热议问题