如何在更改文件名时*不*更改文件扩展名?
How to *not* change the file extension while changing a filename?
我正在尝试从文件名中删除数字。但这样做也会删除扩展名,mp4
文件变成 mp
。什么是解决这个问题的好方法?
import os
def file_rename():
name_list=os.listdir(r"C:\Users\caspe\OneDrive\Documents\Övrigt\Kodning\Twitch")
print(name_list)
saved_path=os.getcwd()
print("Current working directory is"+saved_path)
os.chdir(r"C:\Users\caspe\OneDrive\Documents\Övrigt\Kodning\Twitch")
for file_name in name_list:
print("old name"+file_name)
print("new name"+file_name.strip("0123456789"))
os.rename(file_name, file_name.translate(str.maketrans('','','0123456789-')))
os.chdir(saved_path)
file_rename()
您可以使用 re
,例如
import re, os, glob
for f in glob.glob(os.path.join(yourdir,'*')):
old_name=os.path.basename(f)
old_split = old_name.split('.')
ext = old_split[-1]
new_name=re.sub('\d','',''.join(old_split[:-1])) + '.' + ext
new_f=os.path.join(os.path.dirname(f),new_name)
os.rename(f, new_f)
这里先提取基本名称,然后拆分得到扩展名。接下来,通过将旧基本名称中的所有数字替换为空字符串并添加扩展名来构建新的基本名称。
然后构建完整路径并重命名文件。
类似
remove = set("0123456789-")
fname = 'my_file1-new12.mp4'
parts = fname.split('.')
parts[0] = ''.join(x for x in parts[0] if x not in remove)
clean_fname = parts[0] + '.' + parts[1]
print(clean_fname)
输出
my_filenew.mp4
您可以使用 pathlib.Path 个对象。它有 name
and suffix
attributes, and a rename 方法:
import re
from pathlib import Path
for file in Path(r'C:\tmp').glob('*'):
if not file.is_file():
continue
new_name = re.sub('\d','', file.stem) + file.suffix
file.rename(file.parent/new_name)
parent
attribute gives the folder the file belongs to and the is_file
方法用于检查我们处理的是否是常规文件(而不是文件夹)。使用 /
运算符可以轻松创建新路径对象(完整的新文件路径是 file.parent / new_name
)。
re.sub()
用于替换旧文件名 stem
部分中的数字(\d
表示数字)。
这个简单的正则表达式将解决您在 for 循环中的问题。它将首先在第一个单点之前获取文件名,并将其分组。然后您可以从文件名中删除所有必需的数字以进行重命名。
find = re.compile(r"^[^.]*")
for file_name in name_list:
print (re.search(find, file_name).group(0))
new_name = re.search(find, file_name).group(0)
print("new name are"+ new_name.strip("0123456789"))
我正在尝试从文件名中删除数字。但这样做也会删除扩展名,mp4
文件变成 mp
。什么是解决这个问题的好方法?
import os
def file_rename():
name_list=os.listdir(r"C:\Users\caspe\OneDrive\Documents\Övrigt\Kodning\Twitch")
print(name_list)
saved_path=os.getcwd()
print("Current working directory is"+saved_path)
os.chdir(r"C:\Users\caspe\OneDrive\Documents\Övrigt\Kodning\Twitch")
for file_name in name_list:
print("old name"+file_name)
print("new name"+file_name.strip("0123456789"))
os.rename(file_name, file_name.translate(str.maketrans('','','0123456789-')))
os.chdir(saved_path)
file_rename()
您可以使用 re
,例如
import re, os, glob
for f in glob.glob(os.path.join(yourdir,'*')):
old_name=os.path.basename(f)
old_split = old_name.split('.')
ext = old_split[-1]
new_name=re.sub('\d','',''.join(old_split[:-1])) + '.' + ext
new_f=os.path.join(os.path.dirname(f),new_name)
os.rename(f, new_f)
这里先提取基本名称,然后拆分得到扩展名。接下来,通过将旧基本名称中的所有数字替换为空字符串并添加扩展名来构建新的基本名称。
然后构建完整路径并重命名文件。
类似
remove = set("0123456789-")
fname = 'my_file1-new12.mp4'
parts = fname.split('.')
parts[0] = ''.join(x for x in parts[0] if x not in remove)
clean_fname = parts[0] + '.' + parts[1]
print(clean_fname)
输出
my_filenew.mp4
您可以使用 pathlib.Path 个对象。它有 name
and suffix
attributes, and a rename 方法:
import re
from pathlib import Path
for file in Path(r'C:\tmp').glob('*'):
if not file.is_file():
continue
new_name = re.sub('\d','', file.stem) + file.suffix
file.rename(file.parent/new_name)
parent
attribute gives the folder the file belongs to and the is_file
方法用于检查我们处理的是否是常规文件(而不是文件夹)。使用 /
运算符可以轻松创建新路径对象(完整的新文件路径是 file.parent / new_name
)。
re.sub()
用于替换旧文件名 stem
部分中的数字(\d
表示数字)。
这个简单的正则表达式将解决您在 for 循环中的问题。它将首先在第一个单点之前获取文件名,并将其分组。然后您可以从文件名中删除所有必需的数字以进行重命名。
find = re.compile(r"^[^.]*")
for file_name in name_list:
print (re.search(find, file_name).group(0))
new_name = re.search(find, file_name).group(0)
print("new name are"+ new_name.strip("0123456789"))