I want to return a list based on a directory listing for sorting. My problem is that when I do a loop for each file in that list and attempt to remove based on the user inputted extension with .endswith()
it doesnt seem to update the list correctly (but it does print out "removing file..."). Am I doing something wrong? should I be updating or returning the list (files
) somewhere I am not?>
Also if I set the dirlist variable files
as files=file_list.sort()
it returns none, I have to set files=sorted(file_list)
... why is that?
import os
class Sort:
dirchoice = os.getcwd()
file_list = list(os.listdir(dirchoice))
files=sorted(file_list)
def file_extension(self):
if input("Would you like to specify a particular file extension [Y/N]").lower() == ('y' or 'yes'):
ext=input("filetype>")
ext = str(ext)
for file in self.files:
print(file)
if not file.endswith(ext):
self.files.remove(file)
print("removing " +file+ " from list...")
return self.files
print("File Contents: ")
pprint.pprint(self.files)
return self.file_extension()