使用了python的os模块中的find join rename的方法,对[电影天堂www.dy2018.com]等文件名前缀进行一键修改文件名
主要思路是遍历文件,对符合条件的字符串进行修改
下面给出了代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
import os def visitDir(path): if not os.path.isdir(path): print('Error: "', path, '" is not a directory or does not exist.') return else: global x for lists in os.listdir(path): try: sub_path = os.path.join(path, lists) x += 1 if sub_path.find('[') > 0: index1 = sub_path.index('[') index2 = sub_path.index(']') index3 = sub_path.index('.') if sub_path[index2+1] == '.': newname = sub_path[:index3-1] + sub_path[index3+1:len(sub_path)] else: newname = sub_path[:index1] + sub_path[index2 + 1:len(sub_path)] os.rename(sub_path, newname) print('No.', x, ' ', newname, '修改成功!') if os.path.isdir(sub_path): visitDir(sub_path) except: pass if __name__ == '__main__': x = 0 visitDir('H:\\Movie&Series') |
修改前:
修改后: