How to delete a file or folder?

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

How do I delete a file or folder in Python?

13条回答
  •  长发绾君心
    2020-11-22 13:04

    To remove all files in folder

    import os
    import glob
    
    files = glob.glob(os.path.join('path/to/folder/*'))
    files = glob.glob(os.path.join('path/to/folder/*.csv')) // It will give all csv files in folder
    for file in files:
        os.remove(file)
    

    To remove all folders in a directory

    from shutil import rmtree
    import os
    
    // os.path.join()  # current working directory.
    
    for dirct in os.listdir(os.path.join('path/to/folder')):
        rmtree(os.path.join('path/to/folder',dirct))
    

提交回复
热议问题