在维护原始目录结构的同时将特定的文件移至新目录

发布于 2025-02-13 02:29:16 字数 1455 浏览 0 评论 0原文

我想从命名的目录(及其子目录和文件)以“ cd_”开头,从预定源的源目录中找到的大量“ ab-”目录开始:

src ='/home/home/home/user/source “

到一个新的(目标)目录:

dst ='/home/user/enside''

partial )SRC的目录结构如下:

home/user/source
    ab-0001
        cd_data_1
        cd_data_2
        ef_data_1
        gh_data_1
    ab-0002
        sub_folder
            cd_data_1
            ef_data_1
    ab-0003
        sub_folder
            cd_data_1
        sub_folder_2
            cd_data_1
            ef_data_1

DST文件夹最初是空的,但是在移动文件后,我希望DST和SRC显示如下:

SRC:

home/user/source
    ab-0001
        ef_data_1
        gh_data_1
    ab-0002
        sub_folder
            ef_data_1
    ab-0003
        sub_folder
        sub_folder_2
            ef_data_1

DST:

/home/user/destination
    ab-0001
        cd_data_1
        cd_data_2
    ab-0002
        sub_folder
            cd_data_1             
    ab-0003
        sub_folder
            cd_data_1
        sub_folder_2
            cd_data_1

请注意,SRC中有大量的“ AB-”目录(带有可变结构),因此我们不能对每个目录进行硬编码。

到目前为止,我只能识别需要移动的目录:

 for root_dir_path, sub_dirs, files in os.walk(SRC):
      for dir in sub_dirs:
          if dir.startswith("cd_"):
              print("Root Directory Path:", root_dir_path)
              print("Sub Directories named 'cd_*':", dir)

我坚持如何解决“ AB-”目录的结构的可变性,以根据需要将文件移至DST。

I want to move directories (and their sub-directories and files) that are named starting with "cd_" from a large number of "ab-" directories found within a pre-defined source directory:

SRC = '/home/user/source'

to a new (destination) directory:

DST = '/home/user/destination'

The (partial) directory structure of SRC is as follows:

home/user/source
    ab-0001
        cd_data_1
        cd_data_2
        ef_data_1
        gh_data_1
    ab-0002
        sub_folder
            cd_data_1
            ef_data_1
    ab-0003
        sub_folder
            cd_data_1
        sub_folder_2
            cd_data_1
            ef_data_1

DST folder is initially empty but after moving the files, I want DST and SRC to appear as follows:

SRC:

home/user/source
    ab-0001
        ef_data_1
        gh_data_1
    ab-0002
        sub_folder
            ef_data_1
    ab-0003
        sub_folder
        sub_folder_2
            ef_data_1

DST:

/home/user/destination
    ab-0001
        cd_data_1
        cd_data_2
    ab-0002
        sub_folder
            cd_data_1             
    ab-0003
        sub_folder
            cd_data_1
        sub_folder_2
            cd_data_1

Note that there are a large number of "ab-" directories (with variable structures) in SRC, so we cannot hard code each one.

So far, I have only been able to identify the directories that need to be moved with:

 for root_dir_path, sub_dirs, files in os.walk(SRC):
      for dir in sub_dirs:
          if dir.startswith("cd_"):
              print("Root Directory Path:", root_dir_path)
              print("Sub Directories named 'cd_*':", dir)

I am stuck on how to address the variability in the structure of the "ab-" directories to move the files to DST as desired.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼眸印温柔 2025-02-20 02:29:16

我想你快到了。您正在子目录列表中搜索“ CD_”,但是您必须查看文件列表。假设您的源文件夹仅具有以“ AB-”开头的所有Directory -NAME,则可以在下面尝试使用。

SRC = 'home/user/source'
DEST = 'home/user/target'
for root_dir_path, sub_dirs, files in os.walk(SRC):
      for file_name in files:
          if file_name[:3]=='cd_': # 3 because len('cd_')=3 # string slicing
            # Your required file with cd_ prefix
            src_path = 'home/user'+root_dir_path+file_name
            dest_path = src_path.replace(SRC, DEST, 1)
            # move file
            shutil.move(src_path, dest_path)

希望这就是您想要的。

I think you are almost there. You are searching for 'cd_' in the list of sub directories, but you have to look into the list of files. Assuming your source folder only has all directory -name starting with "ab-", you can try below method.

SRC = 'home/user/source'
DEST = 'home/user/target'
for root_dir_path, sub_dirs, files in os.walk(SRC):
      for file_name in files:
          if file_name[:3]=='cd_': # 3 because len('cd_')=3 # string slicing
            # Your required file with cd_ prefix
            src_path = 'home/user'+root_dir_path+file_name
            dest_path = src_path.replace(SRC, DEST, 1)
            # move file
            shutil.move(src_path, dest_path)

Hope this is what you wanted.

有深☉意 2025-02-20 02:29:16
import os
import shutil

SRC = 'home/user/source'
DST = 'home/user/target'

for root_dir_path, sub_dirs, files in os.walk(SRC):
    for dir in sub_dirs:
        if dir.startswith("cd_"):
            src_path = root_dir_path+"/"+dir
            dest_path = src_path.replace(SRC, DST, 1)
            shutil.move(src_path, dest_path)
import os
import shutil

SRC = 'home/user/source'
DST = 'home/user/target'

for root_dir_path, sub_dirs, files in os.walk(SRC):
    for dir in sub_dirs:
        if dir.startswith("cd_"):
            src_path = root_dir_path+"/"+dir
            dest_path = src_path.replace(SRC, DST, 1)
            shutil.move(src_path, dest_path)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文