根据 If 条件删除当前目录
Delete a current directory based on If condition
以下代码计算每个子目录中的图像数量。如果子目录中的图像超过 2 个,如何删除子目录。
n13 是主目录=> 其中有 300 个子目录(1...300)=> 每个子目录有 图片。
输出:
Images:2、Directory:1
Images:3、Directory:2
Images:4, Directory:3
import os
path='C:/n13/'
def count_em(path):
x = 0
for root, dirs, files in os.walk(path):
files_count = (len(files))
x = x + 1
print("Images:",files_count,"Directory:",x)
return files_count
您可以使用 shutil.rmtree() 删除文件夹及其子目录和文件。
import os
import shutil
path='C:/n13/'
def count_em(path):
x = 0
files_count = 0
for root, dirs, files in os.walk(path):
files_count = (len(files))
if files_count >= 2:
shutil.rmtree(root)
x = x + 1
print("Images:", files_count, "Directory:", x)
return files_count
count_em(path)
以下代码计算每个子目录中的图像数量。如果子目录中的图像超过 2 个,如何删除子目录。
n13 是主目录=> 其中有 300 个子目录(1...300)=> 每个子目录有 图片。
输出:
Images:2、Directory:1
Images:3、Directory:2
Images:4, Directory:3
import os
path='C:/n13/'
def count_em(path):
x = 0
for root, dirs, files in os.walk(path):
files_count = (len(files))
x = x + 1
print("Images:",files_count,"Directory:",x)
return files_count
您可以使用 shutil.rmtree() 删除文件夹及其子目录和文件。
import os
import shutil
path='C:/n13/'
def count_em(path):
x = 0
files_count = 0
for root, dirs, files in os.walk(path):
files_count = (len(files))
if files_count >= 2:
shutil.rmtree(root)
x = x + 1
print("Images:", files_count, "Directory:", x)
return files_count
count_em(path)