用Python批量改名如何跳过非指定后缀名
发布网友
发布时间:2022-04-30 01:05
我来回答
共1个回答
热心网友
时间:2022-04-18 10:42
#!/usr/bin/env python3.6
import os
path = 'D:\\Download\\'
fs = os.listdir(path)
n = 1
for fn in fs:
if not fn.endswith('.PNG'):
continue
newname = f'{n}'.join(os.path.splitext(fn))
os.rename(f'{path}{fn}', f'{path}{newname}')
print(fn, '----->', newname)
n += 1
or
...
fs = [i for i in os.listdir(path) if fn.endswith('.PNG')]
for i, fn in enumerate(fs, 1):
newname = f'{i}'.join(os.path.splitext(fn))
os.rename(f'{path}{fn}', f'{path}{newname}')
print(fn, '----->', newname)
追问invalid syntax
newname = f'{n}'.join(os.path.splitext(fn))
我想问这些f'{n}'、f'{path}{fn}'、 f'{path}{newname}'是什么意思?大括号还有这种用法?