简体   繁体   English

如何调整代码以新的数字开头?

[英]How can i adjust my code to start with a new number?

import os
src = "/home/user/Desktop/images/"
ext = ".jpg"
for i,filename in enumerate(os.listdir(src)):
    # print(i,filename)

    if filename.endswith(ext):
        os.rename(src + filename, src + str(i) + ext)
        print(filename, src + str(i) + ext)
    else :
        os.remove(src + filename)

this code will rename all the images in a folder starting with 0.jpg,1.jpg etc... and remove none jpg but what if i already had some images in that folder, let's say i had images 0.jpg, 1.jpg, 2.jpg, then i added a few others called im5.jpg and someImage.jpg. 此代码将重命名文件夹中以0.jpg,1.jpg等开头的所有图像,并且不删除任何jpg,但是如果我已经在该文件夹中包含一些图像,那么假设我有图像0.jpg,1。 jpg,2.jpg,然后我添加了其他几个,分别称为im5.jpg和someImage.jpg。

What i want to do is adjust the code to read the value of the last image number, in this case 2 and start counting from 3 . 我想要做的是调整代码以读取最后一个图像编号的值,在这种情况下为2,然后从3开始计数。 In other words i'll ignore the already labeled images and proceed with the new ones counting from 3. 换句话说,我将忽略已标记的图像,并继续从3开始计数。

Terse and semi-tested version: 简洁的半测试版本:

import os
import glob
offset = sorted(int(os.path.splitext(os.path.basename(filename))[0]) 
                for filename in glob.glob(os.path.join(src, '*' + ext)))[-1] + 1

for i, filename in enumerate(os.listdir(src), start=offset):
    ...

Provided all *.jpg files consist of a only a number before their extension. 提供的所有* .jpg文件在扩展名前包含一个数字。 Otherwise you will get a ValueError . 否则,您将得到ValueError

And if there happens to be a gap in the numbering, that gap will not be filled with new files. 而且,如果在编号上恰好有间隔,则该间隔将不会填充新文件。 Eg, 1.jpg, 2.jpg, 3.jpg, 123.jpg will continue with 124.jpg (which is safer anyway). 例如,1.jpg,2.jpg,3.jpg,123.jpg会继续显示124.jpg(无论如何还是比较安全)。


If you need to filter out filenames such as im5.jpg or someImage.jpg, you could add an if-clause to the list comprehension, with a regular expression: 如果您需要过滤掉诸如im5.jpg或someImage.jpg之类的文件名,则可以使用正则表达式将if子句添加到列表推导中:

import os
import glob
import re
offset = sorted(int(os.path.splitext(os.path.basename(filename))[0]) 
                for filename in glob.glob(os.path.join(src, '*' + ext))
                if re.search('\d+' + ext, filename))[-1] + 1

Of course, by now the three lines are pretty unreadable, and may not win the code beauty contest. 当然,到现在为止,这三行代码还不太可读,可能无法赢得代码选美大赛。

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

相关问题 如何调整代码以读取其他所有行? - How can i adjust my code to read every other line? 如何调整我的代码以适应 Django 升级? - How can I adjust my code to adapt the Django upgrade? 如何为我的代码的每一行添加一个新数字到我的列表中? - How can I append a new number to my list for every new line for my code? 如何在代码中添加一个显示列数的新行 - How can I add a new row which shows the number of columns into my code 如何将我的代码返回到代码开头? - How can I return my code to the start of the code? 如何调整代码以使其在多个条件下循环返回? - How can I adjust my code to allow it to loop back with multiple conditions? 如何调整我的代码以仅删除打印所需的值 - How can i adjust my code to remove print desired values only 如何调整代码,使唯一的输出是最终迭代? - How can I adjust my code so that the only output is the final iteration? 如何调整此 Newton-Raphson for 循环以包含初始猜测的一系列值,以便打印每个值的迭代次数? - How can I adjust this Newton-Raphson for loop to include a range of values for my initial guess so that it prints the number of iterations for each? 我怎样才能使这部分成为我的新代码循环? - How can i make this part of my new code loop?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM