Pregunta diaria: 01 Cómo agregar "0" + número de serie delante del nombre del archivo en el directorio especificado

import os

path = "C:\\"

i = 1

for filename in os.listdir(path):
    try:
        new_name = "0" + str(i) + "_" + filename
        os.rename(os.path.join(path, filename), os.path.join(path, new_name))
        i += 1
    except PermissionError:
        print("拒绝访问路径,请启用超级权限")
#include <iostream>
#include <filesystem>
#include <string>

namespace fs = std::filesystem;

int main() {
    std::string path = "C:\\";
    int i = 1;

    for (const auto& entry : fs::directory_iterator(path)) {
        try {
            std::string filename = entry.path().filename().string();
            std::string new_name = "0" + std::to_string(i) + "_" + filename;
            fs::rename(entry.path(), entry.path().parent_path() / new_name);
            i++;
        }
        catch (const std::exception& e) {
            std::cout << "Error: " << e.what() << std::endl;
        }
    }

    return 0;
}

Supongo que te gusta

Origin blog.csdn.net/seek97/article/details/129826505
Recomendado
Clasificación