Skip to content

Instantly share code, notes, and snippets.

@KyleJamesWalker
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KyleJamesWalker/13f76797b1311f37a72f to your computer and use it in GitHub Desktop.
Save KyleJamesWalker/13f76797b1311f37a72f to your computer and use it in GitHub Desktop.
Batch Rename as Parent Directory
import os
def main():
'''Stupidly simple script to rename all files to the name of their
parent_dir directory.
Warning: Be careful when running, this could be bad... Also
make sure your current directory doesn't have anything in
it but folders. Otherwise they'll get renamed to ..extention
'''
for dir_name, subdirs, files in os.walk('.'):
parent_dir = dir_name.rsplit('/', 1)[-1]
for fname in files:
new_name = "{}.{}".format(parent_dir, fname.split('.', 1)[-1])
os.rename("{}/{}".format(dir_name, fname),
"{}/{}".format(dir_name, new_name))
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment