Skip to content

Instantly share code, notes, and snippets.

@hussnainsheikh
Created September 6, 2022 00:07
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 hussnainsheikh/078d44432262d5b8d978f5fbbc24136e to your computer and use it in GitHub Desktop.
Save hussnainsheikh/078d44432262d5b8d978f5fbbc24136e to your computer and use it in GitHub Desktop.
Rename the *index.html* files in all directories, sub directories and remove the directories and sub directories.
import os
def getListOfFiles(dirName):
# create a list of file and sub directories
# names in the given directory
listOfFile = os.listdir(dirName)
allFiles = list()
# Iterate over all the entries
for entry in listOfFile:
print(entry)
if entry == 'index.html' and dirName != './':
os.rename(os.path.join(dirName, entry), dirName.replace('./', '')+'.html')
try:
os.rmdir(dirName)
print("% s removed successfully" % dirName)
except OSError as error:
print(error)
print("File path can not be removed")
# Create full path
fullPath = os.path.join(dirName, entry)
# If entry is a directory then get the list of files in this directory
if os.path.isdir(fullPath):
allFiles = allFiles + getListOfFiles(fullPath)
else:
if ('.DS_Store' not in fullPath) and ('rename.py' not in fullPath):
allFiles.append(fullPath)
return allFiles
files = getListOfFiles('./')
print(3*'---'+'Bingo!'+3*'---')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment