mirror of
				https://forge.chapril.org/tykayn/workflow
				synced 2025-10-09 17:02:47 +02:00 
			
		
		
		
	add rename photo folder
This commit is contained in:
		
							parent
							
								
									a610a9f945
								
							
						
					
					
						commit
						706266f970
					
				
					 1 changed files with 141 additions and 0 deletions
				
			
		
							
								
								
									
										141
									
								
								files-management/rename_photo_folder.py
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										141
									
								
								files-management/rename_photo_folder.py
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,141 @@ | |||
| # /home/poule/backup/rename_photos_testland/07 Juillet 2006/ | ||||
| 
 | ||||
| import re | ||||
| import sys | ||||
| import os | ||||
| import os.path | ||||
| import time | ||||
| import logging | ||||
| from optparse import OptionParser | ||||
| import colorama | ||||
| import datetime  # for calculating duration of chunks | ||||
| import json  # to parse JSON meta-data files | ||||
| import appendfilename | ||||
| 
 | ||||
| # définition du script | ||||
| USAGE = "\n\ | ||||
|     rename_photo_folder [<options>] <list of files>\n\ | ||||
| \n\ | ||||
| This little Python script tries to rename files.\n\ | ||||
| \n\ | ||||
| " | ||||
| 
 | ||||
| # gestion des options de commande | ||||
| parser = OptionParser(usage=USAGE) | ||||
| parser.add_option("--debug", dest="debug", action="store_true", | ||||
|                   help="enable debug mode, printing debug information on selected file formats. Currently: just PXL files.") | ||||
| 
 | ||||
| (options, args) = parser.parse_args() | ||||
| files = args | ||||
| 
 | ||||
| # helpers pour clarifier le déroulement | ||||
| INVOCATION_TIME = time.strftime("%Y-%m-%dT%H:%M:%S", time.localtime()) | ||||
| PROG_VERSION_DATE = "0.5.0" | ||||
| 
 | ||||
| # dossier de destination de base pour les fichiers de photos | ||||
| destination_folder_base="/home/poule/backup/rename_photos_testland/" | ||||
| SUCCESS_DIR=destination_folder_base+'success' | ||||
| 
 | ||||
| 
 | ||||
| from pathlib import Path | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
| def move_to_success_dir(dirname, newfilename): | ||||
|     """ | ||||
|     Moves a file to SUCCESS_DIR | ||||
|     """ | ||||
|     if os.path.isdir(SUCCESS_DIR): | ||||
|         logging.debug('using hidden feature: if a folder named \"' + SUCCESS_DIR + | ||||
|                       '\" exists, move renamed files into it') | ||||
|         os.rename(os.path.join(dirname, newfilename), os.path.join(dirname, SUCCESS_DIR, | ||||
|                                                                    newfilename)) | ||||
|         logging.info('moved file to sub-directory "' + SUCCESS_DIR + '"') | ||||
| 
 | ||||
| def make_new_name_from_dirname(filename): | ||||
|     """ | ||||
|     Gets a new name included into a file name | ||||
|     """ | ||||
|     # Split the extension from the path and normalise it to lowercase. | ||||
|     ext_origin = os.path.splitext(filename)[-1] | ||||
|     ext = ext_origin.lower() | ||||
|     current_dir_name = os.path.basename(os.getcwd()) | ||||
| 
 | ||||
|     newfile_name = filename.removesuffix(ext_origin).replace('  ',' ')+' '+current_dir_name+' '+ext | ||||
|     print(filename  +'\n => '+newfile_name) | ||||
|     return newfile_name | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
| import subprocess | ||||
| 
 | ||||
| 
 | ||||
| def move_searched_files_to_target_dir(search_path, target_dir): | ||||
|     my_file_search = Path(search_path) | ||||
|     if my_file_search.exists(): | ||||
|         bash_command="mv "+my_file_search+" "+current_dir_name+"' " | ||||
| 
 | ||||
|         print(bash_command) | ||||
|         process = subprocess.Popen(bash_command.split(), stdout=subprocess.PIPE) | ||||
|         output, error = process.communicate() | ||||
| 
 | ||||
| 
 | ||||
| from string import Template | ||||
| 
 | ||||
| # action principale, lecture du dossier pour changer le nom des fichiers | ||||
| def main(): | ||||
|     """Main function""" | ||||
| 
 | ||||
| 
 | ||||
|     files_counter = 0 | ||||
|     folder_counter = 0 | ||||
|     current_dir_name = os.path.basename(os.getcwd()) | ||||
| #     if options.version: | ||||
| #         print(os.path.basename(sys.argv[0]) + " version " + PROG_VERSION_DATE) | ||||
| #         sys.exit(0) | ||||
| 
 | ||||
| #     print("renommer les photos selon le nom de dossier les contenant") | ||||
| #   print("nom du dossier: ") | ||||
| 
 | ||||
|     print("chemin current_dir_name: ",current_dir_name) | ||||
| 
 | ||||
|     for file in files: | ||||
|         if os.path.isdir(file): | ||||
|             print('hop hop hop voilà un dossier: '+file) | ||||
|             print('on bouge tous les fichiers vidéos qu\'il peut contenir ici') | ||||
| 
 | ||||
|             move_searched_files_to_target_dir(file+'/*.mpg', current_dir_name) | ||||
|             move_searched_files_to_target_dir(file+'/*.mpeg', current_dir_name) | ||||
|             move_searched_files_to_target_dir(file+'/*.mp4', current_dir_name) | ||||
|             folder_counter += 1 | ||||
|     # prendre le path de chaque photo donnée en argument du script | ||||
|     # garder le dernier dossier du path en tant que nom à ajouter aux photos | ||||
|     for file in files: | ||||
|         if os.path.isfile(file) and current_dir_name not in file: | ||||
|             new_file_name = make_new_name_from_dirname(file) | ||||
| 
 | ||||
|             # if the current_dir_name is not already present in the file name, add it | ||||
| 
 | ||||
|             old_file_name = os.getcwd()+'/'+file | ||||
| #             bash_command_line = 'appendfilename "'+fullpathfile+'" --text="'+current_dir_name+'"' | ||||
| 
 | ||||
| #             print(' \n '+bash_command_line+' \n') | ||||
|             if os.stat(old_file_name): | ||||
|                 print('le fichier existe bien '+ old_file_name) | ||||
|                 print('nouveau nom : '+ make_new_name_from_dirname(file)) | ||||
|                 os.rename(old_file_name, make_new_name_from_dirname(file)) | ||||
|                 files_counter += 1 | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
|     # TODO | ||||
| 
 | ||||
|     # faire du append to file name du nom du dossier à chaque photo | ||||
| 
 | ||||
|     print("\n filenames found: " + (str(len(files)))) | ||||
|     print(" filenames changed: " + str(files_counter)) | ||||
|     print(" folders found: " + str(folder_counter)) | ||||
|     print("ça c'est fait") | ||||
| 
 | ||||
| 
 | ||||
| main() | ||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Tykayn
						Tykayn