Skip to content

Instantly share code, notes, and snippets.

@abderhasan
Created March 18, 2021 03:17
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 abderhasan/0a04a544fb00928c532545f8d68c2404 to your computer and use it in GitHub Desktop.
Save abderhasan/0a04a544fb00928c532545f8d68c2404 to your computer and use it in GitHub Desktop.
# A script that moves files to another directory, and renames any files already
# present in the output directory
import os
import os.path
from PIL import Image
import shutil
import argparse as ap
from pathlib import Path
parser = ap.ArgumentParser()
parser.add_argument('-ip', '--inputPath', help='where the files (images) you would like to move reside (source directory)', required='True')
parser.add_argument('-op', '--outputPath', help='where you would like to move the files (destination directory)', required='True')
args = vars(parser.parse_args())
path = args['inputPath']
outputPath = args['outputPath']
i = 0
for root, dirs, files in os.walk(path):
for file in files:
directory_name = os.path.basename(os.path.dirname(root + '/' + file))
file_path_name_input = Path(root + '/' + file)
file_path_name_output = Path(outputPath + '/' + file)
if file_path_name_output.is_file():
file_path = Image.open(outputPath + '/' + file)
print('File path: ')
print(file_path)
if file_path_name_output.is_file():
file_name = os.path.basename(file_path_name_input)
file_name_without_extension = os.path.splitext(file_name)[0]
new_file_name = file_name_without_extension + '_' + str(i) + '.jpg'
print('File name:')
print(file_name)
print('File name without extension: ')
print(file_name_without_extension)
print('New file name: ')
print(new_file_name)
file_path.save(root + '/' + new_file_name)
shutil.move(root + '/' + new_file_name, outputPath)
i = i + 1
else:
shutil.move(root + '/' + file, outputPath)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment