遍历文件夹并为每个文件夹创建新的子文件夹,然后在Python中移动图像

问题描述

假设我有一个包含子文件project1,project2,project3,...文件夹。

enter image description here

在每个项目中,我都有固定命名为processprogresssession的子文件夹,在这些子文件夹中,还有其他子文件夹和图像文件

现在,我想为每个files1创建子文件project,以从process移动所有图像,并创建files2progress移动所有图像}和session

请注意,每个项目的图像名称都是唯一的,因此我们将忽略图像名称重复的问题。

files1创建project1时,我使用:

import os
dirs = './project1/files1'

if not os.path.exists(dirs):
    os.makedirs(dirs)

但是我需要遍历所有项目文件夹。

我们如何在Python中做到这一点?真诚的感谢。

解决方法

为每个file1创建file2project

# Remove non-images files
base_dir = './'
for root,dirs,files in os.walk(base_dir):
    for file in files:
        # print(file)
        pic_path = os.path.join(root,file)
        ext = os.path.splitext(pic_path)[1].lower()
        if ext not in ['.jpg','.png','.jpeg']:
            os.remove(pic_path)
            print(pic_path)

# create files1 and files2
for child in os.listdir(base_dir):
    child_path = os.path.join(base_dir,child)
    os.makedirs(child_path + '/file1',exist_ok=True)
    os.makedirs(child_path + '/file2',exist_ok=True)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...