简体   繁体   English

创建子文件夹并在其中存储指定的文件/图像

[英]Creating subfolder and storing specified files/images in those

During one of my projects, I faced this challenge: There is a folder named Project , and inside that, there are multiple images (say 100 images), and each has been named sequentially like the first image name is imag_0, 2nd image name is img_2,....imag_99.在我的一个项目中,我遇到了这个挑战:有一个名为Project的文件夹,里面有多个图像(比如 100 张图像),并且每个图像都按顺序命名,比如第一个图像名称是 imag_0,第二个图像名称是img_2,....imag_99。 Now, based on some conditions, I need to separate out some images say img_5, img_10, img_30, img_88, img_61.现在,根据某些条件,我需要分离出一些图像,比如 img_5、img_10、img_30、img_88、img_61。 My question will be, is there any way to filter out these images and make a folder inside the folder Project named "the odd ones" and store those specified images?我的问题是,有没有办法过滤掉这些图像并在名为“the odd ones”的文件夹Project中创建一个文件夹并存储那些指定的图像?

One extra help will be in my case.在我的情况下,将有一个额外的帮助。 Suppose I have hundreds of such Projects folders in a sequential way Projects_1 , Projects_2 , Projects_3 ,....., Projects_99 , and each contains hundreds of pictures.假设我有数百个这样的项目文件夹,依次为Projects_1Projects_2Projects_3 、.....、 Projects_99 ,每个文件夹包含数百张图片。 Can it be possible to separate all the specified photos and store them inside a separate folder inside each Projects_n folder, assuming the photos we have to separate out and store differently will be the same for each Projects_n folder?是否可以分离所有指定的照片并将它们存储在每个Projects_n文件夹内的单独文件夹中,假设我们必须分离出来并以不同方式存储的照片对于每个Projects_n文件夹都是相同的? Please help me with this.请帮我解决一下这个。 Thank you!谢谢!

For the first problem you can lookup to the below pseudo-code (you have to specify the target function).对于第一个问题,您可以查找下面的伪代码(您必须指定目标函数)。 Instead, for the second problem you should provide more details;相反,对于第二个问题,您应该提供更多细节;

from glob import glob
import itertools
import shutil
import os

# Creating a funtion to check if filename 
# is a target file which has to be moved:
def is_target(filename):
    if ... return True
    else return False

dirname = "some/path/to/project"

# Creating a list of all files in dir which
# could be moved based on type extension:
types = ('*.png', '*.jpeg')
filepaths = list(itertools.chain(*[glob(os.path.join(dirname, f"*.{t}")) for t in types]))

# Finding the files to move:
filepaths_to_move = []
for filepath in filepaths:
    if is_target(os.path.basename(filepath)):
        filepaths_to_move.append(filepath)

# Creating the new subfolder:
new_folder_name = "odd_images"
new_dir = os.path.join(dirname, new_folder_name)
if not os.path.exists(new_dir): os.makedirs(new_dir)

# Moving files into subfolder:
for filepath in filepaths_to_move:
    basename = os.path.basename(filepath)
    shutil.move(source, os.path.join(filepath, os.path.join(dirname, basename))) 

Here is the logic.make necessary improvements for your use case这是逻辑。对您的用例进行必要的改进

    project_dir = "project_dir"
    move_to_dir = os.path.join(project_dir,"move_to_dir")
    
    files = [os.path.join(project_dir,file) for file in os.listdir(project_dir)]
    filenames_to_filter = "test1.txt,test2.txt"
    if not os.path.exists(move_to_dir):
       os.makedirs(move_to_dir)

    for(file in files):
        if os.path.basename(file) in filenames_to_filter:
           shutil.move(file,move_to_dir)
    `
      

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

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