Reputation: 87
I am still struggling with the idea of OOP but here is my attempt. I have given a search around StackOverflow or OOP readings in general but none quite give me an answer, so I am turning to you for help!
Background
I am trying to place the names of all .csv files in a directory into a list and eventually manipulate them through pandas. The problem is while doing for loop across glob.iglob(os.path.join(dir, ".csv")
and getting the names specific to those files and appending it into the list declared in class Initial()
, I am expecting the names of the files along with .csv to appear when I print init.lst
but instead, it is giving me an empty list.
MY CODE
class Initial():
def __init__(self):
self.lst = []
self.params = []
self.bad_temp = []
self.bad_offsetn = []
self.bad_offsetp = []
self.bad_IT = []
self.choicefile = []
self.finalfile = []
def get_path(self):
while True:
dir = input("Paste full path to directory here: ")
#print(dir)
print(os.path.isdir(dir))
if os.path.isdir(dir) == True:
for files in glob.iglob(os.path.join(dir, ".csv")):
print(files)
longname = os.path.basename(files)
self.lst.append(longname)
return self.lst
break
else:
print('\n')
print('Sorry that is not a valid path/directory. Please try again.')
continue
init = Initial()
init.get_path()
print(init.lst)
print('\n')
print('The following files are found in this directory: ')
for files in init.lst:
print('\t' + files)
What it prints
Paste full path to directory here: dir
[]
The following files are found in this directory:
What I have tried
I have tried printing at every portion of the code that I could print. Everything works up till printing dir
but it somehow doesn't register the for loop.
I tried using this code which is a different approach to getting the list of files in a directory. onlyfiles = [f for f in os.listdir(dir) if os.path.isfile(os.path.join(dir,f))]
. This works and it prints what I need and I can probably tweak it to only get .csv files, but I don't think I can rest until I understand what I did wrong for my initial attempt.
I have also tried moving the function out of the class or remove the function entirely to just working with init.lst
but it still returns empty. (yes, I have os and glob imported)
My take on this
I think my issue is either declaring .lst
in __init__
or not. But from my understanding, it does not really matter as it becomes and instanced variable which requires a self.
to be called upon. And just as the function is defined inside the class itself, it would require me to use self.lst
as part of the code. However, once I go beyond the class and begin using it outside, I would have to attach an object to it which creates the instance for it to be called. Because I think I am right and since it is not working, I know I am wrong and therefore, stuck. Do forgive me if I worded it weirdly or if my understanding is wrong. I would greatly appreciate it if you would correct me if that is the case.
Thank you in advance for helping me out as well as having the patience to teach me!
SOLVED
As pointed out by @Codesidian , for files in glob.iglob(os.path.join(dir, ".csv")):
is missing a pattern for it to start searching for. So the right code would have been for files in glob.iglob(os.path.join(dir, "* .csv")):
where by an *
is added before .csv
. Thank you @Codesidian !
Upvotes: 1
Views: 113
Reputation: 310
This line:
for files in glob.iglob(os.path.join(dir, ".csv")):
The docs that can be found here for glob state that it searches the directory using a pattern string. Specifically:
No tilde expansion is done, but *, ?, and character ranges expressed with [] will be correctly matched.
What you're inputting into the function is dir/.csv
. That isn't a pattern. What you should have added is:
for files in glob.iglob(os.path.join(dir, "*.csv")):
That will be searching with dir/*.csv
. I've tested locally with nothing changed but the asterisk, and it works perfectly.
Upvotes: 1