在维护原始目录结构的同时将特定的文件移至新目录
我想从命名的目录(及其子目录和文件)以“ 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想你快到了。您正在子目录列表中搜索“ CD_”,但是您必须查看文件列表。假设您的源文件夹仅具有以“ AB-”开头的所有Directory -NAME,则可以在下面尝试使用。
希望这就是您想要的。
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.
Hope this is what you wanted.