我想知道重命名目录中所有文件的最简单方法是什么。
我的文件名看起来像这样:
output_original_2018_0729_112557_065.JPG_7c82d91a-6a2a-4f9f-a9ed-ccbc34afe7a9.JPG
output_original_2018_0729_112557_065.JPG_15f07152-76d0-4834-993b-2ca8f589310f.JPG
output_original_2018_0729_112631_070.JPG_cd08da59-0de7-4a94-b769-5450fb80e982.JPG
现在,我想删除开头和结尾部分,并且如果文件名相同的话,还想用一个计数器(第一,第二,第三)重命名文件本身。导致这样的结果:
2018_0729_112557_065_1.JPG
2018_0729_112557_065_2.JPG
2018_0729_112631_070.JPG
预先感谢
编辑:我尝试使用正则表达式来截断第一部分:
for file in *; do echo mv ./"$file" ./"$(echo "$file" | cut -c17-)";
答案 0 :(得分:2)
这应该产生您想要的确切输出:
import os
import re
from collections import defaultdict
folder = "/path/to/out_original/folder"
regex = "output_original_(.+?.JPG)"
p = defaultdict(list)
for x in os.listdir(folder):
try:
p[re.findall(regex, x)[0]].append(x)
except:
pass
for k, v in p.items():
if len(v) == 1:
os.rename(os.path.join(folder, v[0]), os.path.join(folder, k))
else:
count = 1
for x in v:
os.rename(
os.path.join(folder, x),
os.path.join(folder, os.path.splitext(k)[0] + "_{}.JPG".format(count))
)
count += 1
答案 1 :(得分:1)
计数器要求的不完全是,但是我没有时间来解决它:
import re
import os
filenames = [
'output_original_2018_0729_112557_065.JPG_7c82d91a-6a2a-4f9f-a9ed-ccbc34afe7a9.JPG',
'output_original_2018_0729_112557_065.JPG_15f07152-76d0-4834-993b-2ca8f589310f.JPG',
'output_original_2018_0729_112631_070.JPG_cd08da59-0de7-4a94-b769-5450fb80e982.JPG',
]
new_filenames = set()
for filename in filenames:
base_filename = new_filename = re.match(
r'output_original_(.+?)\.JPG',
filename,
).group(1)
count = 1
while new_filename in new_filenames:
new_filename = "{base_filename}_{count}".format(**locals())
count += 1
new_filenames.add(new_filename)
new_filename += ".JPG"
print(filename, '->', new_filename)
os.rename(filename, new_filename)
输出:
output_original_2018_0729_112557_065.JPG_7c82d91a-6a2a-4f9f-a9ed-ccbc34afe7a9.JPG -> 2018_0729_112557_065.JPG
output_original_2018_0729_112557_065.JPG_15f07152-76d0-4834-993b-2ca8f589310f.JPG -> 2018_0729_112557_065_1.JPG
output_original_2018_0729_112631_070.JPG_cd08da59-0de7-4a94-b769-5450fb80e982.JPG -> 2018_0729_112631_070.JPG
答案 2 :(得分:1)
您可以使用bash模式替换和mv --backup=t
重命名重复的文件名:
find . -name "output_original*" | while read -r file; do
origin=$file
file=${file/\.\/output_original_/}
file=${file/JPG*/JPG}
mv --backup=t $origin $file
done
样本输入:
find . -name "output*"
./output_original_2018_0729_112557_065.JPG_15f07152-76d0-4834-993b-2ca8f589310f.JPG
./output_original_2018_0729_112557_065.JPG_7c82d91a-6a2a-4f9f-a9ed-ccbc34afe7a9.JPG
./output_original_2018_0729_112631_070.JPG_cd08da59-0de7-4a94-b769-5450fb80e982.JPG
./output_original_2018_0729_112557_065.JPG_15f07152-76d0-4834-993b-2ca8f589310x.JPG
输出:
$find . -name "2018*"
./2018_0729_112557_065.JPG
./2018_0729_112557_065.JPG.~1~
./2018_0729_112557_065.JPG.~2~
./2018_0729_112631_070.JPG
答案 3 :(得分:1)
@Sergio的想法的解释:
New-NetFirewallRule -Protocol TCP -LocalPort 44369 -Direction Inbound -Action Allow -DisplayName "Allow network TCP on port 44369"
这会将# Make last also numbered
for file in *.JPG.~1~; do
cp --backup=numbered $file ${file/.~1~}
rm ${file/.~1~}
done
变成2018_0729_112557_065.JPG
。
现在,要更改编号方案:
2018_0729_112557_065.JPG.~3~
当然,如果您还有.JPG以外的内容,则需要对其进行修改。