从文件名中取出除数字以外的所有内容
Take everything out of a file name except from numbers
所以我有文件 3D_492.png
,我试图摆脱所有东西 但 最后一个下划线后面的数字。我该怎么做?
我希望3D_492.png
成为492.png
更多例子:
Anniversary_1_Purple_710.png
变成 710.png
它们都在文件夹\Images
中
编辑:我很笨,忘了说我想用新名称重命名文件。
谢谢
这是一种方法,使用 os.path.basename
然后 str.split
来提取最后一个下划线之后的字符:
import os
lst = ['3D_492.png', 'Anniversary_1_Purple_710.png']
res = [os.path.basename(i).split('_')[-1] for i in lst]
print(res)
['492.png', '710.png']
str.rpartition
的完美工作:
>>> s = "3D_492.png"
>>> start, sep, end = s.rpartition('_')
>>> end
'492.png'
保证 return 三个元素,它们和原始字符串相加。这意味着您始终可以获得 "tail":
的第二个元素
>>> 'Anniversary_1_Purple_710.png'.rpartition('_')[2]
'710.png'
拼凑:
import os
os.chdir('\Images')
for old_name in os.listdir('.'):
new_name = old_name.rpartition('_')[2]
if not exists(new_name):
os.rename(old_name, new_name)
您可以使用正则表达式搜索扩展名前面的数字。
import re
def update_name(name):
return re.search(r'\d+\..*', name).group()
update_name('3D_492.png') # '492.png'
使用拆分:
filename = "3D_710.png"
# create a list of the parts of the file name separated by "_"
filename_parts = filename.split("_")
# new_file is only the last part
new_file = filename_parts[-1]
print new_file
# 710.png
包含重命名的完整示例,假设 Images
是相对于包含我们的 Python 脚本的目录:
from os import listdir, rename
from os.path import isfile, join, realpath, dirname
dir_path = dirname(realpath(__file__))
images_path = join(dir_path, "Images")
only_files = [f for f in listdir(images_path) if isfile(join(images_path, f))]
for file in only_files:
filename_parts = file.split("_")
# new_file is only the last part
new_file = filename_parts[-1]
rename(join(images_path, file), join(images_path, new_file))
听起来您只想在 _
上拆分并忽略除最后一个结果之外的所有内容。
*_, result = fname.split("_")
# or:
# result = fname.split("_")[-1]
使用 os.rename
完成重命名
for fname in fnames: # where fnames is the list of the filenames
*_, new = fname.split("_")
os.rename(fname, new)
请注意,如果您想使用绝对路径执行此操作(例如,如果 fnames
看起来像 ["C:/users/yourname/somefile_to_process_123.png", ...]
,则需要使用 os.path.split
进行更多处理)
for fpath in fnames:
*dir, basename = os.path.split(fpath)
*_, newname = basename.split("_")
newpath = os.path.join(dir[0], newname)
os.rename(fpath, newpath)
所以我有文件 3D_492.png
,我试图摆脱所有东西 但 最后一个下划线后面的数字。我该怎么做?
我希望3D_492.png
成为492.png
更多例子:
Anniversary_1_Purple_710.png
变成 710.png
它们都在文件夹\Images
编辑:我很笨,忘了说我想用新名称重命名文件。
谢谢
这是一种方法,使用 os.path.basename
然后 str.split
来提取最后一个下划线之后的字符:
import os
lst = ['3D_492.png', 'Anniversary_1_Purple_710.png']
res = [os.path.basename(i).split('_')[-1] for i in lst]
print(res)
['492.png', '710.png']
str.rpartition
的完美工作:
>>> s = "3D_492.png"
>>> start, sep, end = s.rpartition('_')
>>> end
'492.png'
保证 return 三个元素,它们和原始字符串相加。这意味着您始终可以获得 "tail":
的第二个元素>>> 'Anniversary_1_Purple_710.png'.rpartition('_')[2]
'710.png'
拼凑:
import os
os.chdir('\Images')
for old_name in os.listdir('.'):
new_name = old_name.rpartition('_')[2]
if not exists(new_name):
os.rename(old_name, new_name)
您可以使用正则表达式搜索扩展名前面的数字。
import re
def update_name(name):
return re.search(r'\d+\..*', name).group()
update_name('3D_492.png') # '492.png'
使用拆分:
filename = "3D_710.png"
# create a list of the parts of the file name separated by "_"
filename_parts = filename.split("_")
# new_file is only the last part
new_file = filename_parts[-1]
print new_file
# 710.png
包含重命名的完整示例,假设 Images
是相对于包含我们的 Python 脚本的目录:
from os import listdir, rename
from os.path import isfile, join, realpath, dirname
dir_path = dirname(realpath(__file__))
images_path = join(dir_path, "Images")
only_files = [f for f in listdir(images_path) if isfile(join(images_path, f))]
for file in only_files:
filename_parts = file.split("_")
# new_file is only the last part
new_file = filename_parts[-1]
rename(join(images_path, file), join(images_path, new_file))
听起来您只想在 _
上拆分并忽略除最后一个结果之外的所有内容。
*_, result = fname.split("_")
# or:
# result = fname.split("_")[-1]
使用 os.rename
for fname in fnames: # where fnames is the list of the filenames
*_, new = fname.split("_")
os.rename(fname, new)
请注意,如果您想使用绝对路径执行此操作(例如,如果 fnames
看起来像 ["C:/users/yourname/somefile_to_process_123.png", ...]
,则需要使用 os.path.split
进行更多处理)
for fpath in fnames:
*dir, basename = os.path.split(fpath)
*_, newname = basename.split("_")
newpath = os.path.join(dir[0], newname)
os.rename(fpath, newpath)