我有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中,它只是提取的运行计数和每个文件的编号。
答案 0 :(得分:1)
正如有人所说,“对每个列表进行排序并将它们压缩在一起以便重命名”。
注意:
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))
答案 1 :(得分:0)
您可能希望使用带有文件名模式的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)
希望这有帮助
答案 2 :(得分:0)
我更加完整: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)