Skip to content

Instantly share code, notes, and snippets.

@oyewunmio
Created October 11, 2021 03:50
Show Gist options
  • Save oyewunmio/e078a8c3282fc148da4fd1f199b28108 to your computer and use it in GitHub Desktop.
Save oyewunmio/e078a8c3282fc148da4fd1f199b28108 to your computer and use it in GitHub Desktop.
Stackoverflow answer to renaming directory
# importing modules
import os
import shutil
import sys
#ask user directory
file_dir = input('Enter directory path of containing files to work on only')
#change to working directory
os.chdir(file_dir)
print('Current directory is -->', os.getcwd())
#listing files
for files in os.listdir():
#split filename
remove_imgbit = files.split('.png')[0].split('_')
#remove img8bit
remove_imgbit.remove('leftImg8bit')
#add file name in list back
remove_imgbit = '_'.join(remove_imgbit)
#getting file suffix to separate them into directories
file_suffix = remove_imgbit.split('.png')[0].split('_')[-1]
#create directory
try:
os.mkdir(file_suffix)
except FileExistsError:
print('Folder Exists')
sys.exit() # exit terminal if error occurs
if file_suffix == '0.01':
shutil.move(files, os.path.join(str(file_dir), '0.01'))
elif file_suffix == '0.02':
shutil.move(files, os.path.join(str(file_dir), '0.02'))
elif file_suffix == '0.005':
shutil.move(files, os.path.join(str(file_dir), '0.005'))
###To run file
# 1. install python on your cent os (if you dont already have)
# 2. run python rename.py in your terminal
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment