简体   繁体   English

使用文件夹的名称重命名文件夹中的所有图像

[英]renaming all images in folders using the name of the folder

I have a lot of folders. 我有很多文件夹。 in each folder, there is an image. 在每个文件夹中,都有一个图像。 I want to rename all of the images with the name of its folder. 我想用其文件夹的名称重命名所有图像。

For example: Folder "1" has an image "273.jpg" I want to change it into the "1.jpg" and save it in another directory. 例如:文件夹“1”有一个图像“273.jpg”我想将其更改为“1.jpg”并将其保存在另一个目录中。

This is what I have done so far: 这是我到目前为止所做的:

import os
import pathlib

root = "Q:/1_Projekte/2980/"

for path, subdirs, files in os.walk(root):
    for name in files:
        print (pathlib.PurePath(path, name))
        os.rename(os.path.join(path, name), os.path.join(path, os.path.basename(path)))
        print(os.path.basename(path))

The problem is that it works just for the first folder, then it jumps out with the error: 问题是它只适用于第一个文件夹,然后它跳出错误:

this file is already available...

the tree of folders and the images are like this: 文件夹树和图像是这样的:

Q:/1_Projekte/2980/1/43425.jpg 问:/1_Projekte/2980/1/43425.jpg

Q:/1_Projekte/2980/2/43465.jpg 问:/1_Projekte/2980/2/43465.jpg

Q:/1_Projekte/2980/3/43483.jpg 问:/1_Projekte/2980/3/43483.jpg

Q:/1_Projekte/2980/4/43499.jpg 问:/1_Projekte/2980/4/43499.jpg

So there is just one file in each directory! 所以每个目录中只有一个文件!

Probably you have some hidden files in those directories. 可能你在这些目录中有一些隐藏文件。 Check it out. 看看这个。 If those files are not jpg , you can use following code: 如果这些文件不是jpg ,您可以使用以下代码:

for path, subdirs, files in os.walk(root):
    for name in files:
        extension = name.split(".")[-1].lower()
        if extension != "jpg":
            continue
        os.rename(os.path.join(path, name), os.path.join(path, os.path.basename(path) + "." + extension))
        print(os.path.basename(path))

This code extracts the extension of the file and checks if it is equal to the jpg . 此代码提取文件的扩展名并检查它是否等于jpg If file extension is not jpg , so continue statement will run and next file will check. 如果文件扩展名不是jpg ,那么将运行continue语句并检查下一个文件。 If file type is jpg , script renames it. 如果文件类型是jpg ,则脚本重命名它。 Also this code adds original file extension to the new name. 此代码还将原始文件扩展名添加到新名称。 Previous code didn't handle that. 以前的代码没有处理。 I hope this helpe you. 我希望这能帮到你。

Maybe this might help... 也许这可能有帮助......

import os

root = "Q:/1_Projekte/2980/"
subdirs = [x for x in os.listdir(root) if os.path.isdir(x)]

for dir_name in subdirs:
    dir_path = root + dir_name + '/'
    files = os.listdir(dir_path)

    print(dir_name)
    print(files)

    for i in files:
        counter = 1
        extension = i.split('.')[-1]
        new_name = dir_name + '.' + extension

        while True:
            try:
                os.rename(dir_path + i, dir_path + new_name)
                break

            except:
                # If the file exists...
                new_name = dir_name + '({})'.format(counter) + '.' + extension
                counter += 1

This code ensures that even if a file with the existing name happens to exist, it will be suffixed with a number in brackets. 此代码确保即使具有现有名称的文件恰好存在,也会在括号中以数字为后缀。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM