Skip to content

Instantly share code, notes, and snippets.

@leonardreidy
Last active August 9, 2021 12:35
Show Gist options
  • Save leonardreidy/2524c7ac6f28efb35b02419e312e048e to your computer and use it in GitHub Desktop.
Save leonardreidy/2524c7ac6f28efb35b02419e312e048e to your computer and use it in GitHub Desktop.
Python Housekeeping Utilities
import os
# For every file in the current directory, replace '.js' in the filename with an empty
# string if the file is not a directory
[os.rename(f, f.replace('.js', '')) for f in os.listdir('.') if not os.path.isdir(f)]
# For every file in every subdirectory of the target directory (targetDir), replace '.js' in the filename
# with an empty string if the file is not a directory
def renameDoubleBarrelledCoffeeScriptsIn(targetDir):
for root, subdirs, files in os.walk(targetDir):
os.chdir(root)
[os.rename(f, f.replace('.js', '')) for f in os.listdir('.') if not os.path.isdir(f)]
# Starting in the current directory, rename all double barrelled CoffeeScript script files
renameDoubleBarrelledCoffeeScriptsIn(os.getcwd())
@leonardreidy
Copy link
Author

Generalise this function for batch renaming of files by providing a pattern as a parameter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment