如何在 Python 中以相反的顺序重命名文件?

How to rename files in reverse order in Python?

我有一台扫描仪,可以创建一个图像文件夹,名称如下:

A1.jpg A2.jpg A3.jpg...A24.jpg -> B1.jpg B2.jpg B3.jpg...B24.jpg

A1到P24共16行,每行24张图片,共384张图片。

我想通过颠倒顺序来重命名它们。第一个文件应采用最后一个文件的名称,反之亦然。首先考虑是A1(也是扫描时创建的第一个)

我能找到的最接近的示例在 中,但这并不是我真正想要的:

for i in {1..50}; do
    mv "$i.txt" "renamed/$(( 50 - $i + 1 )).txt"
done

也许我需要将文件名保存到列表中(也许是 natsort?)然后以某种方式使用这些名称?

我还认为我可以使用图像创建时间,因为扫描仪总是以相同的顺序创建具有相同名称的文件。话虽如此,任何解决方案对于面临同样挑战的其他人来说可能都没有那么有用。

解决这个问题的明智方法是什么?

我会存储原始列表。然后以相同的顺序重命名所有文件(例如 1.jpg、2.jpg 等)。然后我将所有这些文件重命名为原始列表的反面。

这样在重命名时就不会遇到重复的文件名了。

您可以为此使用 pathlib 函数 renameiterdir。我认为如何将它们放在一起很简单。

我不知道这是否是最佳方式,但这里是:

import os

folder_name = "test"
new_folder_name = folder_name + "_new"

file_names = os.listdir(folder_name)
file_names_new = file_names[::-1]
print(file_names)
print(file_names_new)

os.mkdir(new_folder_name)

for name, new_name in zip(file_names, file_names_new):
    os.rename(folder_name + "/" + name, new_folder_name + "/" + new_name)

os.rmdir(folder_name)
os.rename(new_folder_name, folder_name)

这假定您已将文件保存在目录“test”中

基于 shutil 包的解决方案(os 包有时有权限问题)和“就地”如果文件夹很大不浪费内存

import wizzi_utils as wu
import os


def reverse_names(dir_path: str, temp_file_suffix: str = '.temp_unique_suffix') -> None:
    """
    "in place" solution:
        go over the list from both directions and swap names
        swap needs a temp variable so move first file to target name with 'temp_file_suffix'
    """
    files_full_paths = wu.find_files_in_folder(dir_path=dir_path, file_suffix='', ack=True, tabs=0)
    files_num = len(files_full_paths)

    for i in range(files_num):  # works for even and odd files_num
        j = files_num - i - 1
        if i >= j:  # crossed the middle - done
            break
        file_a, file_b = files_full_paths[i], files_full_paths[j]
        print('replacing {}(idx in dir {}) with {}(idx in dir {}):'.format(
            os.path.basename(file_a), i, os.path.basename(file_b), j))
        temp_file_name = '{}{}'.format(file_b, temp_file_suffix)
        wu.move_file(file_src=file_a, file_dst=temp_file_name, ack=True, tabs=1)
        wu.move_file(file_src=file_b, file_dst=file_a, ack=True, tabs=1)
        wu.move_file(file_src=temp_file_name, file_dst=file_b, ack=True, tabs=1)
    return


def main():
    reverse_names(dir_path='./scanner_files', temp_file_suffix='.temp_unique_suffix')
    return


if __name__ == '__main__':
    main()

found 6 files that ends with  in folder "D:\workspace21wizzi_utils\temp\Whosebug\scanner_files":
    ['A1.jpg', 'A2.jpg', 'A3.jpg', 'B1.jpg', 'B2.jpg', 'B3.jpg']
replacing A1.jpg(idx in dir 0) with B3.jpg(idx in dir 5):
    D:/workspace/2021wizzi_utils/temp/Whosebug/scanner_files/A1.jpg Moved to D:/workspace/2021wizzi_utils/temp/Whosebug/scanner_files/B3.jpg.temp_unique_suffix(0B)
    D:/workspace/2021wizzi_utils/temp/Whosebug/scanner_files/B3.jpg Moved to D:/workspace/2021wizzi_utils/temp/Whosebug/scanner_files/A1.jpg(0B)
    D:/workspace/2021wizzi_utils/temp/Whosebug/scanner_files/B3.jpg.temp_unique_suffix Moved to D:/workspace/2021wizzi_utils/temp/Whosebug/scanner_files/B3.jpg(0B)
replacing A2.jpg(idx in dir 1) with B2.jpg(idx in dir 4):
    D:/workspace/2021wizzi_utils/temp/Whosebug/scanner_files/A2.jpg Moved to D:/workspace/2021wizzi_utils/temp/Whosebug/scanner_files/B2.jpg.temp_unique_suffix(0B)
    D:/workspace/2021wizzi_utils/temp/Whosebug/scanner_files/B2.jpg Moved to D:/workspace/2021wizzi_utils/temp/Whosebug/scanner_files/A2.jpg(0B)
    D:/workspace/2021wizzi_utils/temp/Whosebug/scanner_files/B2.jpg.temp_unique_suffix Moved to D:/workspace/2021wizzi_utils/temp/Whosebug/scanner_files/B2.jpg(0B)
replacing A3.jpg(idx in dir 2) with B1.jpg(idx in dir 3):
    D:/workspace/2021wizzi_utils/temp/Whosebug/scanner_files/A3.jpg Moved to D:/workspace/2021wizzi_utils/temp/Whosebug/scanner_files/B1.jpg.temp_unique_suffix(0B)
    D:/workspace/2021wizzi_utils/temp/Whosebug/scanner_files/B1.jpg Moved to D:/workspace/2021wizzi_utils/temp/Whosebug/scanner_files/A3.jpg(0B)
    D:/workspace/2021wizzi_utils/temp/Whosebug/scanner_files/B1.jpg.temp_unique_suffix Moved to D:/workspace/2021wizzi_utils/temp/Whosebug/scanner_files/B1.jpg(0B)