Python script to rename images

I like to collect images for my desktop background, the problem is sometimes the image names don’t represent what the image is. I decided to write a script that reads a text file that contains the source of the images (folders stored in the Pictures directory under the Userprofile (windows)) to be renamed.

The full path to the folder listed below are:

C:\Users\Kiska\Pictures\Landscape C:\Users\Kiska\Pictures\Batman 

Rather than have the user type the useprofile path every time they want to add a folder to the list, the Folder.py class does it for them, they just provide the sub directory to be processed.

Source (.txt):

Landscape Batman 

PathUtilities.py:

def verify_parent_directory(parent: str) -> None:     parent = parent.strip()     path_without_drive_letter = parent[2:]      _determine_if_drive_letter_is_valid(file_path=parent)     _check_if_string_ends_with_slash(string_to_validate=path_without_drive_letter)      if len(path_without_drive_letter) > 2:         _check_for_invalid_characters(string_to_validate=path_without_drive_letter)   def verify_subdirectory(subdirectory: str) -> None:     subdirectory = subdirectory.strip()     _check_if_string_starts_with_slash(string_to_validate=subdirectory)     _check_for_invalid_characters(string_to_validate=subdirectory)     _check_if_string_ends_with_slash(string_to_validate=subdirectory)   def _determine_if_drive_letter_is_valid(file_path: str):     drive_letter_with_colon = file_path[:2]      if not drive_letter_with_colon[0].isalpha():         raise TypeError("Drive Letter is invalid.")      if drive_letter_with_colon[1] != ":":         raise ValueError(f"Second element is invalid. Character(s): {drive_letter_with_colon[1]}")   def _check_for_invalid_characters(string_to_validate : str):      """     Determine if the string contains forbidden elements.      Raises a ValueError if any forbidden character is found.      Args:         string_to_validate : str - The string we're going to test.     """      forbidden_characters = ["<", ">", ":", "/", '"', "|", "?", "*", "\\"]      for forbidden_character in forbidden_characters:         if forbidden_character in string_to_validate:             raise ValueError(f"Invalid characters in path. Character(s): {forbidden_character}")   def _check_if_string_starts_with_slash(string_to_validate : str):     if string_to_validate.startswith("\"):         raise ValueError("Invalid characters in path. Character(s): \")   def _check_if_string_ends_with_slash(string_to_validate : str):     if string_to_validate.endswith("\"):         raise ValueError("Invalid characters in path. Character(s): \") 

I created the above module because I might have more projects that require validation of paths.

Folder.py:

import pathutilities import os   class Folder:      def __init__(self, parent: str, subdirectory: str):         pathutilities.verify_parent_directory(parent=parent)         pathutilities.verify_subdirectory(subdirectory=subdirectory)          self._parent = parent         self._subdirectory = subdirectory      @property     def parent(self):         return self._parent      @property     def subdirectory(self):         return self._subdirectory      def construct_path(self) -> str :         return os.path.join(self._parent, self._subdirectory)      def __eq__(self, other):         if isinstance(other, Folder):             return (self.parent, self.subdirectory) == (other.parent, other.subdirectory)         return NotImplemented      def __hash__(self):         return hash((self._parent, self._subdirectory)) 

Repository.py:

from Folder import Folder import os import shutil   class Repository:      def __init__(self, source: Folder, destination: Folder):         if source == destination:             raise ValueError("Source folder cannot be the destination folder")         self._source = source         self._destination = destination      def copy_files_with(self, extension: str):         if extension.startswith("."):             raise ValueError("extension doesn't require a period")          source = self._source.construct_path()         destination = self._destination.construct_path()         number_of_images_in_source = self._get_number_of_images_in_directory(directory=source)          if number_of_images_in_source:             print(f"Copying images from Recent Questions - Code Review Stack Exchange to {destination}\nNumber of images: {number_of_images_in_source}")             os.makedirs(destination, exist_ok=True)              number_of_images_in_destination = self._get_number_of_images_in_directory(directory=destination) + 1              for number, image in enumerate(os.listdir(source), start=number_of_images_in_destination):                 if image.endswith(extension) or image.endswith(extension.upper()):                     source_image = os.path.join(source, image)                     destination_image = os.path.join(destination,                                                      self._construct_destination_string(current_number=number,                                                                                         extension=extension))                     print(f"Copying {source_image} to {destination_image}")                     shutil.move(source_image, destination_image)         else:             print("No images to copy")      def _get_number_of_images_in_directory(self, directory: str) -> int:         return len(os.listdir(directory))      def _construct_destination_string(self, current_number, extension):         return "{0}_{1}.{2}".format(self._source.subdirectory.lower().replace(" ","_"), current_number, extension) 

Main.py:

import sys import os from Folder import Folder from Repository import Repository   def main():      try:          source = "{0}\{1}".format(os.getenv("USERPROFILE"), "Pictures")         destination = "R:\Pictures"          source_list = "source.txt"          with open(source_list) as folders_from_source:             for subfolder in folders_from_source:                 subfolder = subfolder.strip()                 source_folder = Folder(parent=source, subdirectory=subfolder)                 destination_folder = Folder(parent=destination, subdirectory=subfolder)                 repository = Repository(source=source_folder, destination=destination_folder)                 repository.copy_files_with(extension="jpg")      except (TypeError, ValueError, FileExistsError) as error:         print(error)     finally:         sys.exit()   if __name__ == '__main__':     main() 

Suppose there were two images in each of the source folders, it will rename them like this:

landscape_1.jpg landscape_2.jpg  batman_1.jpg batman_2.jpg 

Areas of Concern:

  • Is my code clean? Descriptive variable and method methods, small classes and at least for me, it’s easy to follow.

  • I didn’t include docstrings to save space, but I’m well aware I should include them.

How to move (rename) a file with the URL using JavaScript

I am using SharePoint Office 365. I have tried to extrapolate how to do this from many other sources but I am stuck. I have the URL of a file (happens to be a OneNote Notebook) and I want to move the file to a new name.

This is my last iteration of my code. We already have the context in code above this segment

var onenote_file = target_person_folder + '/RenameMe';     var File = context.get_web().getFileByServerRelativeUrl(onenote_file);                     context.load(File);                     console.log(File);                     var onenote_file_target_name = target_person_folder + '/' + first_name + ' ' + last_name;                     console.log(onenote_file_target_name);                     SP.File.moveTo(context, onenote_file,onenote_file_target_name, 1);                     context.executeQueryAsync(                         function (sender, args) {                             console('We renamed the file. YAY! ')                         },                         function (sender, args) {                             console('BOO!, We did not move the file. .')                         }); 

I think I am close but I would appreciate the help getting the rest of the way there.

Thanks!

Rename multiple folders, using regex

As the title says, I’m looking for a command / tool – preferably something running from the terminal (I’m using Ubuntu on a remote server), that can replace a specific part of multiple directories at once. The folder structure is like this:

Example.of.Structure[app]  Another.Example[app]  Third.Example.of.Structure[app] 

And I want to rename all of the folders, so the [app] part gets removed from the names of the folders.

I’ve tried dabbling with basic RegEx. Matching the square brackets, and the text inside it.

Which seems to work fine, when I use it for “finding it” – but I’m really unsure how to put it into a command.

The regex I came up with, to find the text inside the brackets (including the brackets themselves) was: \[\w+\]

But how do I put this into a command?

Thanks.

Script to Duplicate Template Tab Multiple Times and Rename Tabs from Cell Range?

Basically I have a list of 200 people’s names in the range 'Names'!A1:A200. I have another tab named Template. I need a script to duplicate Template 200 times and rename each of the tabs to the name of each of the people in the range 'Names'!A1:A200. Is this possible? Thank you in advance!

Google Sheets: Script to Create New Sheets from Template + Rename and Auto-populate with Data from Rows?

I have a sheet with many entries from form submissions, each submission with its own row.

I have another sheet that is a template to display the data from each submission.

I would like to have a script duplicate the template, rename the sheet based on the name of the person submitting, and import the data from the submission into various cells on the duplicated template to be able to present it in a better format.

Handle file conflicts last while transferring data. (+rename automatically)

Let’s assume I want to copy or move many files from my mobile phone to the PC overnight.

I go to bed and wake up next morning, just to read a message that states “Error while copying ……… – libmtp error: Could not get file from device.”, that came at 3% and halted the file transfer. So one tiny fault delayed the entire progress (the other 97%) of the file copying.

It would be better if it could first copy all files that can be copied, and then handle file conflicts (existing filename, unsupported characters for target file system, other errors) last, so that it can first get all the work done and then handle the faults.

It would also be good if I could tell the file copier in advance to rename existing files automatically, so that I do not have to wait for the file copier to encounter these files so I can tell it to rename files automatically.

File saving dialogue: ask to rename instead of replace

When I want to save a file to an existing destination, the file saving manager (“Save as”) asks me whether I wish to overwrite the existing file with the same name. The button “yes” is preselected, so one accidential press on space bar or enter could lead to data loss. (Happened to me once in 2014, but then never again.)

I would prefer it to ask me to rename the file automatically or even rename it without asking me, so that I don’t have to take care each time I want to save a file.