How to remove a directory including all its files in python?

前端 未结 3 1818
鱼传尺愫
鱼传尺愫 2020-12-30 01:58

I\'m working on some Python code. I want to remove the new_folder including all its files at the end of program.

Can someone please guide me how I can d

3条回答
  •  别那么骄傲
    2020-12-30 02:46

    If you want to delete the file

    import os
    os.remove("path_to_file")
    

    but you can`t delete directory by using above code if you want to remove directory then use this

    import os
    os.rmdir("path_to_dir")
    

    from above command, you can delete a directory if it's empty if it's not empty then you can use shutil module

    import shutil
    shutil.rmtree("path_to_dir")
    

    All above method are Python way and if you know about your operating system that this method depends on OS all above method is not dependent

    import os
    os.system("rm -rf _path_to_dir")
    

提交回复
热议问题