I know there’s a better way to do this, but I don’t know what it is. I’m sorting through a list of files, and I would like to remove ‘the usual suspects’ so I can compare one list to another.
From what I understand, name.replace() look at each and every item in the listToClean for the phrases I picked, and replace them if present. There has to be a better way to do this…
def cleanLists(listToClean, extList):
cleanFileList = []
for filename in listToClean:
name = os.path.split(filename)[1]
ext = os.path.splitext(name)
if ext[1] in extList:
name = name.replace(ext[1], '')
name = name.replace('1080p', '')
name = name.replace('1080P', '')
name = name.replace('720p', '')
name = name.replace('720P', '')
name = name.replace('HD', '')
name = name.replace('(', ' ')
name = name.replace(')', '')
name = name.replace('.', ' ')
cleanFileList.append(name)
cleanFileList.sort(key=lambda x: x.lower())
return cleanFileList
Consider where ‘csv’ is in your list of extensions and you have a file named ‘summary_of_csv_files.csv’ …