如何在 Python 中删除文件或文件夹? [英] How to delete a file or folder in Python?

查看:30
本文介绍了如何在 Python 中删除文件或文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Python 中删除文件或文件夹?

How do I delete a file or folder in Python?

推荐答案

  • os.remove() 删除文件.

    os.rmdir() 删除一个空目录.

    os.rmdir() removes an empty directory.

    shutil.rmtree() 删除目录及其所有内容.

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

    Path 对象来自Python 3.4+ pathlib 模块也公开了这些实例方法:

    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() 删除一个空目录.

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

    这篇关于如何在 Python 中删除文件或文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆