[英]Rename files in a folder based on names in a different folder
我有2个文件夹,每个文件夹具有相同数量的文件。 我想根据文件夹1中文件的名称来重命名文件夹2中的文件。因此,文件夹1中可能有三个标题为:
Landsat_1,Landsat_2,Landsat_3
在文件夹2中,这些文件称为:
一二三
我想根据文件夹1的名称重命名它们。 我考虑过将每个文件夹的项目名称转换为一个.txt文件,然后将.txt文件转换为列表,然后重命名,但是我不确定这是否是最好的方法。 有什么建议么?
编辑:
我已经简化了上面的文件名,因此仅添加Landsat_对我不起作用。
文件夹1中的真实文件名更像LT503002011_band1,LT5040300201_band1,LT50402312_band4。 在文件夹2中,它们分别是extract1,extract2,extract3。 总共有500个文件,在文件夹2中,这只是一个提取的运行计数,每个文件都有一个数字。
就像有人说的那样,“将每个列表排序并压缩在一起以便重命名”。
笔记:
key()
函数提取所有数字,以便sorted()
可以根据嵌入的数字对列表进行数字排序。 os.listdir()
以任意顺序返回文件。 for
循环是使用zip的一种常见方式: for itemA, itemB in zip(listA, listB):
os.path.join()
提供可移植性:不用担心/
或\\
python doit.py c:\\data\\lt c:\\data\\extract
,假设这些是您描述的目录。 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))
您可能想要使用采用文件名模式并将其输出到列表的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)
希望这可以帮助
我去争取更完整: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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.