在更改文件名时不更改文件扩展名,可以通过编程方式实现。以下是一个使用Python语言的示例代码,展示了如何实现这一功能:
import os
def change_filename_without_extension(file_path, new_filename):
# 获取文件的目录和扩展名
directory, filename_with_extension = os.path.split(file_path)
name, extension = os.path.splitext(filename_with_extension)
# 构建新的文件名
new_filename_with_extension = new_filename + extension
# 构建新的完整文件路径
new_file_path = os.path.join(directory, new_filename_with_extension)
# 重命名文件
os.rename(file_path, new_file_path)
return new_file_path
# 示例用法
file_path = 'path/to/your/file.txt'
new_filename = 'new_file_name'
new_file_path = change_filename_without_extension(file_path, new_filename)
print(f'File has been renamed to: {new_file_path}')
mv
命令)来实现。通过上述方法,可以在更改文件名时保持文件扩展名不变。
领取专属 10元无门槛券
手把手带您无忧上云