Last active
August 9, 2021 12:35
-
-
Save leonardreidy/2524c7ac6f28efb35b02419e312e048e to your computer and use it in GitHub Desktop.
Python Housekeeping Utilities
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Generalise this function for batch renaming of files by providing a pattern as a parameter.