1
$\begingroup$

I'm trying to use the output of glob.glob() as the input to os.listdir() in order to get the number of files in the directory. The output of glob() gives the following:

f = glob.glob(ct)
print(f)

['C:\\Users\\tennant\\Desktop\\RF WAVEFORMS\\SPRING 2018\\RF\\1L22\\2018_05_02\\133258.2']

which, if I try to use as input to listdir() gives the following error

test = os.listdir(str(f))

enter image description here

Eventually through enough trial and error, I was able to find a solution.

f = glob.glob(ct)
fnew = str(f).strip('[]')
test = os.listdir(fnew.replace('\'',""))
print(fnew.replace('\'',""), len(test))

C:\\Users\\tennant\\Desktop\\RF WAVEFORMS\\SPRING 2018\\RF\\1L22\\2018_05_02\\133258.2 7

However it's messy and I'm clearly not understanding something more fundamental about the output of glob() or strings in general. Anything that could clean this code up and help my understanding would be greatly appreciated!

$\endgroup$
5
  • $\begingroup$ f is a list (see help(glob.glob)). Instead of converting it to a string and then stripping the brackets, why don't you just get the element with f[0]? Have you tried os.listdir(f[0])? $\endgroup$
    – mapto
    Mar 12, 2019 at 14:27
  • $\begingroup$ @mapto, thank you - this does work! Appreciate your feedback. $\endgroup$ Mar 12, 2019 at 14:58
  • $\begingroup$ What do you actually want to do? Just count the number of files in a directory? Your glob.glob(ct) doesn't show you are doing any fancy filtering or anything. Or do you want to get the number of files in many directories? $\endgroup$
    – n1k31t4
    Mar 12, 2019 at 16:54
  • $\begingroup$ @n1k31t4, yeah I just want to count files in a directory. ct is a string that contains wildcard characters. $\endgroup$ Mar 13, 2019 at 17:20
  • 1
    $\begingroup$ @ChrisTennant - in that case, if glob is already returning the filenames that you want (i.e. that match you wildcard expression) then you can simply use the length of the returned list: len(f). If it is the case that each element in the list f is a folder, and you want to count the number of files in each of those folders, then just loop over f after performing the glob. I'll add an answer with example code. $\endgroup$
    – n1k31t4
    Mar 13, 2019 at 17:29

1 Answer 1

1
$\begingroup$

Assuming you have a string to pass into glob that does wildcard matching, glob will return a list of matches. So you don't need to make a string out of that list and replace the square-brackets and so on. You can just iterate over that list and do something with each of the values, which are already strings.

results = glob.glob(your_pattern)

Based on your code, the results is actually a list of folder names for which you want to count the number of files in each. We can loop over results and print the counts:

for folder in results:
    n = len(os.listdir(folder))
    print("Folder: {0} --> \t {1} files".format(folder, n))
$\endgroup$
1
  • $\begingroup$ Thanks @n1k31t4, this does exactly what I want. Much appreciated! $\endgroup$ Mar 15, 2019 at 0:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.