根据不同文件夹中的名称重命名文件夹中的文件

Rename files in a folder based on names in a different folder

我有 2 个文件夹,每个文件夹都有相同数量的文件。我想根据文件夹 1 中文件的名称重命名文件夹 2 中的文件。因此在文件夹 1 中可能有三个文件,标题为:

Landsat_1, Landsat_2, Landsat_3

在文件夹 2 中,这些文件名为:

1, 2、 3

我想根据文件夹 1 的名称重命名它们。我考虑过将每个文件夹的项目名称转换为 .txt 文件,然后将 .txt 文件转换为列表,然后重命名,但我不确定这是否是最好的方法。有什么建议么?

编辑:

我已经简化了上面的文件名,所以只是附加 Landsat_ 对我来说不起作用。

文件夹 1 中的真实文件名更像是 LT503002011_band1、LT5040300201_band1、LT50402312_band4。在文件夹 2 中,它们是 extract1、extract2、extract3。总共有 500 个文件,在文件夹 2 中,它只是 运行 个提取物和每个文件的编号。

您可能想要使用 glob 包,它采用文件名模式并将其输出到列表中。比如在那个目录

glob.glob('*')

给你

['Landsat_1', 'Landsat_2', 'Landsat_3']

然后您可以遍历列表中的文件名并相应地更改文件名:

import glob
import os

folderlist = glob.glob('*')
for folder in folderlist:
    filelist = glob.glob(folder + '*')
    for fil in filelist:
         os.rename(fil, folder + fil)

希望对您有所帮助

正如某人所说,"sort each list and zip them together in order to rename"。

备注:

  • key() 函数提取所有数字,以便 sorted() 可以根据嵌入的数字对列表进行数字排序。
  • 我们对两个列表进行排序:os.listdir() returns 个文件以任意顺序排列。
  • for 循环是使用 zip 的常见方式:for itemA, itemB in zip(listA, listB):
  • os.path.join() 提供可移植性:不用担心 /\
  • 对 Windows 的典型调用:python doit.py c:\data\lt c:\data\extract,假设这些是您描述的目录。
  • *nix 上的典型调用::python doit.py ./lt ./extract

import sys
import re
import os

assert len(sys.argv) == 3, "Usage: %s LT-dir extract-dir"%sys.argv[0]
_, ltdir, exdir = sys.argv

def key(x):
    return [int(y) for y in re.findall('\d+', x)]
ltfiles = sorted(os.listdir(ltdir), key=key)
exfiles = sorted(os.listdir(exdir), key=key)

for exfile,ltfile in zip(exfiles, ltfiles):
    os.rename(os.path.join(exdir,exfile), os.path.join(exdir,ltfile))

我追求更完整 :D.

# WARNING: BACKUP your data before running this code. I've checked to
# see that it mostly works, but I would want to test this very well
# against my actual data before I trusted it with that data! Especially
# if you're going to be modifying anything in the directories while this
# is running. Also, make sure you understand what this code is expecting
# to find in each directory.

import os
import re


main_dir_demo = 'main_dir_path'
extract_dir_demo = 'extract_dir_path'


def generate_paths(directory, filenames, target_names):
    for filename, target_name in zip(filenames, target_names):
        yield (os.path.join(directory, filename),
               os.path.join(directory, target_name))


def sync_filenames(main_dir, main_regex, other_dir, other_regex, key=None):
    main_files = [f for f in os.listdir(main_dir) if main_regex.match(f)]
    other_files = [f for f in os.listdir(other_dir) if other_regex.match(f)]

    # Do not proceed if there aren't the same number of things in each
    # directory; better safe than sorry.
    assert len(main_files) == len(other_files)

    main_files.sort(key=key)
    other_files.sort(key=key)

    path_pairs = generate_paths(other_dir, other_files, main_files)
    for other_path, target_path in path_pairs:
        os.rename(other_path, target_path)


def demo_key(item):
    """Sort by the numbers in a string ONLY; not the letters."""
    return [int(y) for y in re.findall('\d+', item)]


def main(main_dir, extract_dir, key=None):
    main_regex = re.compile('LT\d+_band\d')
    other_regex = re.compile('extract\d+')

    sync_filenames(main_dir, main_regex, extract_dir, other_regex, key=key)


if __name__ == '__main__':
    main(main_dir_demo, extract_dir_demo, key=demo_key)