How to use glob() output as input to os.listdir()












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!










share|improve this question









$endgroup$












  • $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
    2 days ago












  • $begingroup$
    @mapto, thank you - this does work! Appreciate your feedback.
    $endgroup$
    – Chris Tennant
    2 days ago










  • $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
    2 days ago










  • $begingroup$
    @n1k31t4, yeah I just want to count files in a directory. ct is a string that contains wildcard characters.
    $endgroup$
    – Chris Tennant
    yesterday










  • $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
    yesterday
















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!










share|improve this question









$endgroup$












  • $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
    2 days ago












  • $begingroup$
    @mapto, thank you - this does work! Appreciate your feedback.
    $endgroup$
    – Chris Tennant
    2 days ago










  • $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
    2 days ago










  • $begingroup$
    @n1k31t4, yeah I just want to count files in a directory. ct is a string that contains wildcard characters.
    $endgroup$
    – Chris Tennant
    yesterday










  • $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
    yesterday














1












1








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!










share|improve this question









$endgroup$




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!







python data






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 2 days ago









Chris TennantChris Tennant

153




153












  • $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
    2 days ago












  • $begingroup$
    @mapto, thank you - this does work! Appreciate your feedback.
    $endgroup$
    – Chris Tennant
    2 days ago










  • $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
    2 days ago










  • $begingroup$
    @n1k31t4, yeah I just want to count files in a directory. ct is a string that contains wildcard characters.
    $endgroup$
    – Chris Tennant
    yesterday










  • $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
    yesterday


















  • $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
    2 days ago












  • $begingroup$
    @mapto, thank you - this does work! Appreciate your feedback.
    $endgroup$
    – Chris Tennant
    2 days ago










  • $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
    2 days ago










  • $begingroup$
    @n1k31t4, yeah I just want to count files in a directory. ct is a string that contains wildcard characters.
    $endgroup$
    – Chris Tennant
    yesterday










  • $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
    yesterday
















$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
2 days ago






$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
2 days ago














$begingroup$
@mapto, thank you - this does work! Appreciate your feedback.
$endgroup$
– Chris Tennant
2 days ago




$begingroup$
@mapto, thank you - this does work! Appreciate your feedback.
$endgroup$
– Chris Tennant
2 days ago












$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
2 days ago




$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
2 days ago












$begingroup$
@n1k31t4, yeah I just want to count files in a directory. ct is a string that contains wildcard characters.
$endgroup$
– Chris Tennant
yesterday




$begingroup$
@n1k31t4, yeah I just want to count files in a directory. ct is a string that contains wildcard characters.
$endgroup$
– Chris Tennant
yesterday












$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
yesterday




$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
yesterday










1 Answer
1






active

oldest

votes


















0












$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)





share|improve this answer









$endgroup$













  • $begingroup$
    Thanks @n1k31t4, this does exactly what I want. Much appreciated!
    $endgroup$
    – Chris Tennant
    3 hours ago











Your Answer





StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
});
});
}, "mathjax-editing");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "557"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdatascience.stackexchange.com%2fquestions%2f47162%2fhow-to-use-glob-output-as-input-to-os-listdir%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0












$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)





share|improve this answer









$endgroup$













  • $begingroup$
    Thanks @n1k31t4, this does exactly what I want. Much appreciated!
    $endgroup$
    – Chris Tennant
    3 hours ago
















0












$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)





share|improve this answer









$endgroup$













  • $begingroup$
    Thanks @n1k31t4, this does exactly what I want. Much appreciated!
    $endgroup$
    – Chris Tennant
    3 hours ago














0












0








0





$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)





share|improve this answer









$endgroup$



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)






share|improve this answer












share|improve this answer



share|improve this answer










answered yesterday









n1k31t4n1k31t4

6,3712319




6,3712319












  • $begingroup$
    Thanks @n1k31t4, this does exactly what I want. Much appreciated!
    $endgroup$
    – Chris Tennant
    3 hours ago


















  • $begingroup$
    Thanks @n1k31t4, this does exactly what I want. Much appreciated!
    $endgroup$
    – Chris Tennant
    3 hours ago
















$begingroup$
Thanks @n1k31t4, this does exactly what I want. Much appreciated!
$endgroup$
– Chris Tennant
3 hours ago




$begingroup$
Thanks @n1k31t4, this does exactly what I want. Much appreciated!
$endgroup$
– Chris Tennant
3 hours ago


















draft saved

draft discarded




















































Thanks for contributing an answer to Data Science Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


Use MathJax to format equations. MathJax reference.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdatascience.stackexchange.com%2fquestions%2f47162%2fhow-to-use-glob-output-as-input-to-os-listdir%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to label and detect the document text images

Vallis Paradisi

Tabula Rosettana