Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@AllanChain
Created September 27, 2020 01:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AllanChain/e923317a381b7de12ba06a0b7ff0f8f5 to your computer and use it in GitHub Desktop.
Save AllanChain/e923317a381b7de12ba06a0b7ff0f8f5 to your computer and use it in GitHub Desktop.
Removes parentheses in filename. Very useful for files downloaded from course.pku.edu.cn
from sys import argv
import os
import re
pattern = re.compile(r'^(.*?)(\(\d+\))+\.(.*?)$')
if __name__ == "__main__":
for file_to_rename in argv[1:]:
if os.path.isfile(file_to_rename):
match_result = re.match(pattern, file_to_rename)
if match_result is not None:
g = match_result.groups()
os.rename(file_to_rename, f'{g[0]}.{g[2]}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment