1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
#!/usr/bin/python2 """ @author: Vasanthi Vuppuluri Date created: April 10, 2015 Purpose: -------- - To rename a directory - Copy content from another directory to the renamed another Executing the script: --------------------- python rename_copy_directories.py /Users/username/Desktop/directory_to_be_renamed new_name /Users/username/Desktop/copy_from Output: ------- - The original folder is renamed to what is specified and the files and sub-folders are recursively copied to the renamed folder Sample input and output: ------------------------ - rename_directory_or_file() Example directory (or) file path: /Users/username/Desktop/directory_to_be_renamed new_name => The directory or file will be renamed to new_name Example: new_name = hello Output: /Users/username/Desktop/hello - copy_contents() shutil module cannot copy contents from source folder to destination folder if the \ destination folder or file is already present. Hence, we first recursively delete the\ folder to which we want the contents to be copied to. It is ok to delete in this case, as we are essentially renaming an empty folder. Contents are now copied to the newly renamed folder. """ import os import sys import errno import shutil def rename_directory_or_file(directory_path, new_name): # Changing the path of the directory first # We can do this by replacing the text after the last occurance of '/' in the path # assuming the path is something similar to: /Users/username/Desktop/directory_to_be_renamed # This method can rename both files and directories renamed_directory_path = '/'.join(directory_path.split('/')[:-1]) + '/' + new_name os.rename(directory_path, renamed_directory_path) return renamed_directory_path def copy_contents(source_directory, renamed_directory_path): # Copies the files and folders from a specfied folder to the renamed folder try: # This copies directories only shutil.copytree(source_directory, renamed_directory_path) except OSError as e: # Here we check if the error was a result of the source not being a directory if e.errno == errno.ENOTDIR: shutil.copy(source_directory, renamed_directory_path) else: print "Copy unsuccessfull!!" print str(e) def main(): directory_path = sys.argv[1] # Directory whose name is to be changed new_name = sys.argv[2] # Name of the directory to which it has to be changed to source_directory = sys.argv[3] # Path of the directory to copy the contents from try: renamed_directory_path = rename_directory_or_file(directory_path, new_name) print "Renamed successfully!! \nNew path: %s" %(renamed_directory_path) except Exception, e: print str(e) try: shutil.rmtree(renamed_directory_path) copy_contents(source_directory, renamed_directory_path) print "Recursively copied the directory contents!!" except Exception, e: print str(e) if __name__ == "__main__": main() |
Leave a Reply