#! python3
# *-* coding:utf-8 *-* #
'''
Description : rename.py is used for batch renaming the file. The interactive info contain:
argv 1: Absolute path which contains all the files you want to rename
'''
import os
import shutil
import re
def get_new_file_name(abs_dir)->list:
os.chdir(abs_dir)
file_name = ""
file_list = []
# find the espisode number in file's name by regex "S01E02","第1集"
ep_regex = re.compile(r'(E|EP|\u7B2C)(\d){1,3}(\u96C6|\u8BDD|\u56DE)?',re.IGNORECASE)
# solve the group(2) not able to fetch digits above 10
# "EP10" regex.groups() like ('E', '0')
num_regex = re.compile(r'(\d){1,3}')
try:
# find the drama name like "GameofThrone"
drama_name = os.path.basename(os.getcwd())
for file_name in os.listdir(abs_dir):
ep_num = ep_regex.search(file_name)
if not ep_num:
print ("The episode num is not found!\n")
continue
else:
#print (ep_num.group())
#print (num_regex.search(ep_num.group()).group())
tmp = num_regex.search(ep_num.group()).group()
# fill the number from '1' to '01' or '001'
if len(os.listdir(abs_dir)) < 100:
episode = tmp.zfill(2)
else:
episode = tmp.zfill(3)
# assemble new name like "GameOfThrone" + "_EP" + "01" + ".mp4"
file_new_name = drama_name + "_EP" + episode + "." + file_name.split('.')[-1]
print ("rename {src_file} to {dst_file}\n".format(src_file=file_name,dst_file=file_new_name))
file_list.append(file_new_name)
except(SystemError,ValueError) as reason:
print (reason)
return -1
return file_list
def rename_files(abs_dir, file_list):
os.chdir(abs_dir)
file_name = ""
for file_name,file_new_name in zip(os.listdir(abs_dir),file_list):
shutil.move(file_name,file_new_name)
for file_name in os.listdir(abs_dir):
print (file_name + "\n")
'''
main fuction
'''
if __name__ == '__main__':
while True:
# input the path and validate it
abs_os_path = input("please input the absolute path containg all the files:\n")
if not os.path.exists(abs_os_path):
print("The path you input doesn't exist\n")
continue
new_name_list = []
new_name_list = get_new_file_name(abs_os_path)
rename_flag = input("Are you sure you going rename all the files?(y/n):\n")
if rename_flag == 'y':
rename_files(abs_os_path,new_name_list)
没有合适的资源?快使用搜索试试~ 我知道了~
资源详情
资源评论
资源推荐
收起资源包目录
rename_file_v1.1.zip (3个子文件)
rename_file_v1.1
rename.py 2KB
auto_rename.py 3KB
使用说明Readme(很重要!).docx 15KB
共 3 条
- 1
refine19
- 粉丝: 19
- 资源: 10
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0