简体   繁体   English

删除Python中的子文件夹

[英]Removing sub-folders in Python

I have an main folder(map) under this main have sub folder (zoom1,zoom2,zoom3...) how can i remove sub folder using shutil. 我有一个主文件夹(地图)下这个主要有子文件夹(zoom1,zoom2,zoom3 ...)如何使用shutil删除子文件夹。 note * : I know the main folder path sub folders are dynamically created note *:我知道主文件夹路径子文件夹是动态创建的

If you're using linux you could do the following. 如果您使用的是Linux,则可以执行以下操作。 Use python's glob library 使用python的glob

Lets you have a directory structure with the following structure. 允许您拥有具有以下结构的目录结构。

  • /map /地图

    • /map/zoom1/ /图/缩放1 /

    • /map/zoom2/ /图/缩放2 /

    • /map/zoom3/ /图/ zoom3 /

Using glob and shutil 使用globshutil

import glob
import shutil

sub_folders_pathname = '/map/zoom*/'
sub_folders_list = glob.glob(sub_folder_pathname)
for sub_folder in sub_folders_list:
    shutil.rmtree(sub_folder)

sub_folders_pathname is a shell-style wildcard, glob supports shell-style wildcards. sub_folders_pathname是一个shell样式的通配符, glob支持shell样式的通配符。

sub_folders_list are a list of folders and then we use shutil.rmtree to remove it. sub_folders_list是文件夹列表,然后我们使用shutil.rmtree删除它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM