希望有用户输入重命名批照片文件,但随尾后缀的变化。
每隔几个月,我就会得到同样的工作,重命名数百张照片。我要花上几个小时,如果不是几天的话。到目前为止,我的代码要求测试类型(照片正在捕获)、测试的数量、测试的深度以及用户输入的深度。
但我遇到了一个障碍,我想能批重命名,但不同的照片显示不同的深度。例如,我希望照片是名字: be 01_0-5m,然后下一张照片命名。BH01 01_5-10m
但是我只知道如何对它进行编码,所以所有的东西都被命名为5m 01_0-5m
这是到目前为止用于用户输入的代码:
borehole = raw_input("What type of geotechnical investigation?")
type(borehole)
number = raw_input("What number is this test?")
type(number)
frommetre = raw_input("From what depth (in metres)?")
type(frommetre)
tometre = raw_input("What is the bottom depth(in metres)?")
type(tometre)
name = (borehole+number+"_"+frommetre+"-"+tometre)
print(name)
我得到了我想要的第一个照片文件的标题,但是如果我在每个文件夹中有4张照片,他们现在将被重命名为与用户输入完全相同的东西。我希望把后缀按5米顺序排列(0-5,5-10,10-15,15-20,20-25等等)。
发布于 2019-05-08 04:05:44
我在这里做了一些假设:
您想要做的事情可以在两个嵌套循环中完成:
下面是一个例子:
from pathlib import Path
from shutil import move
root_folder = 'c:\\temp'
for folder in Path(root_folder).iterdir():
if folder.is_dir():
startDepth = 0
step = 5
for file in Path(folder).iterdir():
if file.is_file():
new_name = f'{folder.name}_{str(startDepth).zfill(3)}-{str(startDepth + step).zfill(3)}{file.suffix}'
print(f'would rename {str(file)} to {str(file.parent / Path(new_name))}')
# move(str(file), str(file.parent / Path(new_name)))
startDepth += step
请注意,我还向每个深度添加了.zfill(3)
,因为我认为您会更喜欢像BH01_000-005.jpg
这样的名称,而不是BH01_0-5.jpg
,因为它们会更好地排序。
注意,这个脚本只输出它要做的事情,您可以简单地注释掉print
语句并删除move
语句前面的注释符号,它实际上会重命名文件。
https://stackoverflow.com/questions/56033043
复制相似问题