简体   繁体   English

按递增顺序重命名多个不同扩展名的同名文件

[英]Rename multiple file of different extension with the same name in the incremental order

I have multiple folders in which I have files named as follows:我有多个文件夹,其中的文件命名如下:

   1500001.jpg
   1500001.xml
   1600002.jpg
   1600002.xml
   9876543.jpg
   9876543.xml

As you can see I have one jpg file and its corresponding xml file with the same file name.如您所见,我有一个 jpg 文件及其对应的 xml 文件,文件名相同。 I want to rename all these files as follows:我想将所有这些文件重命名如下:

   ID_0001.jpg (1500001.jpg converted to ID_0001.jpg)
   ID_0001.xml (1500001.xml converted to ID_0001.xml)
   ID_0002.jpg (1600002.jpg converted to ID_0002.jpg)
   ID_0002.xml (1600002.xml converted to ID_0002.xml
   ID_0003.jpg (9876543.jpg converted to ID_0003.jpg)
   ID_0003.xml (9876543.xml converted to ID_0003.xml)

I am new to Python and have written some code for renaming files with different extension but not able to scale this to keeping the same filename for its corresponding file in the incremental order like ID_0001.jpg, ID_0001.xml, etc.我是 Python 的新手,并且已经编写了一些代码来重命名具有不同扩展名的文件,但无法将其扩展为以增量顺序保持其相应文件的相同文件名,例如 ID_0001.jpg、ID_0001.xml 等。

import os
_src = "path/to/directory"
_ext_jpg = ".jpg"
_ext_xml = ".xml"

for i,filename in enumerate(os.listdir(_src)):
    if filename.endswith(_ext_jpg):
        os.rename(filename, _src+'ID_' + str(i)+_ext_jpg)

    else:
        os.rename(filename, _src+'ID_' + str(i)+_ext_xml)

With this code, the increment happens for the all the files like ID_0001.jpg, ID_002.xml, ID_003.jpg, ID_004.xml and actually it should be like ID_0001.jpg, ID_0001.xml, ID_0002.jpg, ID_0002.xml, etc. With this code, the increment happens for the all the files like ID_0001.jpg, ID_002.xml, ID_003.jpg, ID_004.xml and actually it should be like ID_0001.jpg, ID_0001.xml, ID_0002.jpg, ID_0002.xml,等等

When you get a filename, rename both the xml and jpg files:获得文件名后,重命名xmljpg文件:

import os
_src = "path/to/directory"
_ext_jpg = ".jpg"
_ext_xml = ".xml"

for i,filename in enumerate(os.listdir(_src)):
    if filename.endswith(_ext_jpg):
        os.rename(filename, _src+'ID_' + str(i)+_ext_jpg)
        os.rename(filename[:-4]+_ext_xml, _src+'ID_' + str(i)+_ext_xml)

Using glob使用glob

Ex:前任:

import os
import glob
_src = "path/to/directory"

for i,filename in enumerate(glob.glob("{}*.jpg".format(_src))):
    file = os.path.basename(filename)
    f, ext = os.path.splitext(file)
    os.rename(filename, os.path.join(_src, 'ID_' + str(i)+ext))   #Rename .jpg
    os.rename(os.path.join(_src, "{}.xml".format(f) , os.path.join(_src, 'ID_' + str(i)+".xml"))  #Rename .xml

暂无
暂无

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

相关问题 如何将子文件夹中所有特定文件类型的文件重命名为具有相同文件扩展名但文件名相同的名称 - How to rename all specific filetypes of files within subfolders to name with same file extension but same file name 如何在python中批量重命名具有相同名称和不同扩展名的多个文件 - How to bulk rename multiple files with same name and different extensions in python 读取多个同名但扩展名不同的文件 Python - read multiple files with same name but different extension Python 如何在不更改文件扩展名的情况下从文件夹重命名多个文件名 - how to rename a multiple file name from a folder without changing a file extension Python os.rename with datetime 不断保存多个同名文件 - Python os.rename with datetime keeps saving multiple file same name 重命名一个新文件,当有相同名称的文件时,没有限制名称 - Rename a new file when there in file with the same name without limited name 输入两个名称相同但扩展名不同的文件 - Input two files with same name but different extension 从具有相同名称但文件类型不同的其他文件中查找没有扩展名的文件 - Finding one file without extension from other files with same name but different file type 无法重命名扩展文件 - Impossible to rename extension file 如果已存在同名文件,则将文件移动到文件夹并重命名 - move a file to a folder and rename it if a file with the same name already exists
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM