Skip to content

Instantly share code, notes, and snippets.

@sumanchapai
Last active July 21, 2023 12:20
Show Gist options
  • Save sumanchapai/43a505b44f4e840a15e673d883de8fce to your computer and use it in GitHub Desktop.
Save sumanchapai/43a505b44f4e840a15e673d883de8fce to your computer and use it in GitHub Desktop.
Rename
from pathlib import Path
folder = "/home/LC/chapsu01/raw_data/Summer 2012/April 16, 2012/m23"
folder = Path(folder)
# Use * to select all files
# Use prefixes and suffixes followed/preceded by * to select files
# matching that pattern. For example dark*.fit to select all files whose name
# start with 'dark' and ends with '.fit'
files_to_select = "m23*"
replacee = "3.5" # The thing to replace (old part)
replacer = "7.0" # The thing to replace by (new part)
def main():
if not folder.exists():
print(f"{folder} doesn't exist")
return
paths = {}
for f in folder.glob(files_to_select):
name = f.name
new_name = name.replace(replacee, replacer)
if name != new_name:
print("Old:", name)
print("New:", new_name)
print("-"*max(len(new_name), len(name)))
paths[f] = new_name
else:
print("Old:", name)
print("No change")
if input("Do you want to confirm (Y/N): ").upper() in ["YES", "Y"]:
for p in paths:
new_path = folder / paths[p]
p.rename(new_path)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment