有没有办法可以通过用户输入批量重命名文件夹中的文件?
Is there a way I can batch rename files in a folder with user input?
希望让用户输入重命名批处理照片文件,但更改结尾后缀。
每隔几个月我就会得到一份相同的工作,重新命名数百张照片。如果不是几天的话,我需要几个小时。到目前为止,我有代码询问测试类型(照片正在捕获)、测试次数、测试从用户输入到用户输入的深度。
但是我遇到了一个障碍,我想要能够批量重命名但是不同的照片显示不同的深度。所以例如我希望照片是名字:
BH01_0-5m
然后命名下一张照片。
BH01_5-10m
但我只知道如何编码,所以一切都被命名 BH01_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 等)使后缀连续。
我在这里做一些假设:
- 文件夹的名称就是钻孔的名称
- 每个钻孔的文件名可能不同,但排序时 alpha-numerically,第一个将是最接近地表的文件名
- 所有组都需要增加 5 米
您要执行的操作可以在两个嵌套循环中完成:
- 所有文件夹:
- 对于每个文件夹中的所有文件:
- 重命名文件以匹配文件夹名称和一些深度顺序
这是一个例子:
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
语句前面的注释符号,它实际上会重命名文件。
希望让用户输入重命名批处理照片文件,但更改结尾后缀。
每隔几个月我就会得到一份相同的工作,重新命名数百张照片。如果不是几天的话,我需要几个小时。到目前为止,我有代码询问测试类型(照片正在捕获)、测试次数、测试从用户输入到用户输入的深度。
但是我遇到了一个障碍,我想要能够批量重命名但是不同的照片显示不同的深度。所以例如我希望照片是名字: BH01_0-5m 然后命名下一张照片。 BH01_5-10m
但我只知道如何编码,所以一切都被命名 BH01_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 等)使后缀连续。
我在这里做一些假设:
- 文件夹的名称就是钻孔的名称
- 每个钻孔的文件名可能不同,但排序时 alpha-numerically,第一个将是最接近地表的文件名
- 所有组都需要增加 5 米
您要执行的操作可以在两个嵌套循环中完成:
- 所有文件夹:
- 对于每个文件夹中的所有文件:
- 重命名文件以匹配文件夹名称和一些深度顺序
这是一个例子:
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
语句前面的注释符号,它实际上会重命名文件。