Last active
August 29, 2015 14:15
-
-
Save KyleJamesWalker/13f76797b1311f37a72f to your computer and use it in GitHub Desktop.
Batch Rename as Parent Directory
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 | |
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