Skip to content

Instantly share code, notes, and snippets.

@rs77
Last active January 8, 2023 05:19
Show Gist options
  • Save rs77/c8a19fdef0b381c83ffcc8ed6e32924d to your computer and use it in GitHub Desktop.
Save rs77/c8a19fdef0b381c83ffcc8ed6e32924d to your computer and use it in GitHub Desktop.
I had to rename the folders in a sub-directory on my Mac OS to lower case and this was the only code I could get to work. It appears you need to remove the directory and create a new one with the right case, you cannot simply use `os.rename`
import glob
import pathlib
import shutil
import os
src = '/Users/rds/Local Sites/biblejot/app/public_static/kjv/**/index.html'
temp = '/Users/rds/Local Sites/biblejot/app/public_static/kjv/temp/'
path = pathlib.Path(temp)
path.mkdir(parents=True, exist_ok=True)
files = glob.glob(src, recursive=True)
for f in files:
folders = f.split("/")
old_dir = "/".join(folders[:-1])
new_dir = folders[:-2] + [folders[-2].lower()]
temp_file = os.path.join(temp, folders[-1])
print(f"{f} >> {temp_file}")
shutil.move(f, temp_file)
# delete folder
shutil.rmtree(old_dir, True)
# create new folder
new_path = "/".join(new_dir)
path = pathlib.Path(new_path)
path.mkdir(parents=True, exist_ok=True)
# append file
new_path = os.path.join(new_path, folders[-1])
print(f"{temp_file} >> {new_path}")
shutil.move(temp_file, new_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment