重命名文件为目录名的ID,很多嵌套文件

Rename files as the ID from the directory name, many nested files

我想重命名以 .txt 结尾的文件,这些文件深深嵌套在一组文件中,基于目录的名称,但只取“_”之前的那个,因为它是 ID,因此创建“ID_TreatedSubject.txt”文件

这是我必须开始的:(这是一个较长的列表,仅供参考)

/Users/Owner/Desktop/test/Blood2/4BA(ID)_Kidney_Blood_CEL-archive/Processed/FP004BA Experiment Group (Blood, RMA)/TreatedSubject.txt

/Users/Owner/Desktop/test/Blood2/4BA(ID)_Kidney_Blood_CEL-archive/Processed/FP004BA Experiment Group (Kidney, RMA)/TreatedSubject.txt

期望的输出:

/Users/Owner/Desktop/test/Blood2/4BA(ID)_Kidney_Blood_CEL-archive/Processed/FP004BA Experiment Group (Blood, RMA)/4BA_TreatedSubject.txt

/Users/Owner/Desktop/test/Blood2/4BA(ID)_Kidney_Blood_CEL-archive/Processed/FP004BA Experiment Group (Kidney, RMA)/4BA_TreatedSubject.txt

这是尝试:

import os
def list_files(dir):
    sub_folders = os.listdir(dir)
#     print(sub_folders)
    for sub_folder in sub_folders:
         sub_folder_path = os.path.join(dir,sub_folder)
         for root, dirs, files in os.walk(sub_folder_path):
#             print(type(root))
            for file in files:
                if file.endswith(".txt"):
                    a_string = root
                    partitioned_string = a_string.partition('_')
                    print(partitioned_string)
                    root = before_first_period = partitioned_string[0] 
                    new_filename = sub_folder + file
#                         print(new_filename)
                    os.rename(os.path.join(root, file), os.path.join(root, new_filename))
input_dir = "/Users/Owner/Desktop/test/Blood2/"
assert os.path.isdir(input_dir),"Enter a valid directory path which consists of sub-directories"
list_files(input_dir)

这是我遇到的错误:

('/Users/Owner/Desktop/test/Blood2/4BA', '_', 'Kidney_Blood_CEL-archive/Processed/FP004BA Experiment Group (Blood, RMA)')
---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
<ipython-input-128-53f8924d30fa> in <module>
     19 input_dir = "/Users/Owner/Desktop/test/Blood2/"
     20 assert os.path.isdir(input_dir),"Enter a valid directory path which consists of sub-directories"
---> 21 list_files(input_dir)

<ipython-input-128-53f8924d30fa> in list_files(dir)
     16                     new_filename = sub_folder + file
     17 #                         print(new_filename)
---> 18                     os.rename(os.path.join(root, file), os.path.join(root, new_filename))
     19 input_dir = "/Users/Owner/Desktop/test/Blood2/"
     20 assert os.path.isdir(input_dir),"Enter a valid directory path which consists of sub-directories"

FileNotFoundError: [Errno 2] No such file or directory: '/Users/Owner/Desktop/test/Blood2/4BA/TreatedSubject.txt' -> '/Users/Owner/Desktop/test/Blood2/4BA/4BA_Kidney_Blood_CEL-archiveTreatedSubject.txt'

如果我删除这个块:

a_string = root
partitioned_string = a_string.partition('_')
print(partitioned_string)
root = before_first_period = partitioned_string[0] 

我得到以下输出:

/Users/Owner/Desktop/test/Blood2/4BA_Kidney_Blood_CEL-archive/Processed/FP004BA Experiment Group (Blood, RMA)/4BA_Kidney_Blood_CEL-archiveTreatedSubject.txt

/Users/Owner/Desktop/test/Blood2/4BA_Kidney_Blood_CEL-archive/Processed/FP004BA Experiment Group (Kidney, RMA)/4BA_Kidney_Blood_CEL-archiveTreatedSubject.txt

我只需要它从“_”中拆分出来,而不是添加整行。任何帮助将不胜感激,我觉得我很接近!

os.walk 将使这变得相当简单。

如果我没理解错的话,ID 位于第一个 _ 和其后续 / 路径分隔符之间。所以我们可以在使用 .partition 获取字符串的相关部分后使用 .split('/').split('_', 1) 也可以)

因为os.walk已经将路径与文件名分开,我们可以通过在现有文件名前加上ID和下划线来构造一个新的文件名。

import os

def get_id(fp):
    return fp.partition('_')[0].split('/')[-1]


def rename_files(root_dir):
    for root, dirs, files in os.walk(root_dir):
        for fname in files:
            if fname.endswith('.txt'):
                fp = os.path.join(root, fname)
                id_ = get_id(fp)
                new_fname = f'{id_}_{fname}'
                new_fp = os.path.join(root, new_fname)
                print('renaming', fp, 'to', new_fp)
                # os.rename(fp, new_fp)

rename_files('/Users/Owner/Desktop/test/')