Skip to content

Instantly share code, notes, and snippets.

@johnpili
Last active February 27, 2022 14:08
Show Gist options
  • Save johnpili/57e1a33a69d36ea6e712ebc954a77b5e to your computer and use it in GitHub Desktop.
Save johnpili/57e1a33a69d36ea6e712ebc954a77b5e to your computer and use it in GitHub Desktop.
Rename files by removing the starting and matching string
import os
import sys
from os import path
parameters = sys.argv[1:]
if len(parameters) == 0:
print(f"usage: {sys.argv[0]} <startswith-string>")
sys.exit(0)
if len(parameters) > 0:
for file in os.listdir():
if file.startswith(parameters[0]) and path.isfile(file):
old_name = file
new_name = file[len(parameters[0]):]
print(f"Renaming {old_name} -> {new_name}")
os.renames(old_name, new_name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment