Created
September 6, 2022 00:07
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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