How to delete a file or folder?

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

How do I delete a file or folder in Python?

13条回答
  •  悲哀的现实
    2020-11-22 13:13

    I recommend using subprocess if writing a beautiful and readable code is your cup of tea:

    import subprocess
    subprocess.Popen("rm -r my_dir", shell=True)
    

    And if you are not a software engineer, then maybe consider using Jupyter; you can simply type bash commands:

    !rm -r my_dir
    

    Traditionally, you use shutil:

    import shutil
    shutil.rmtree(my_dir) 
    

提交回复
热议问题