How to use glob() output as input to os.listdir()Tensorflow neural network TypeError: Fetch argument has invalid typeImage as input and output in kerasHow do I provide input and output for such a network structure in kerasTensorFlow: Regression using Deep Neural NetworkMatrix Factorisation ImprovementHow to use the time-sampled data(50 samples/Minute) as input for classifying the outputSeparate scalers for input and outputUsing LSTM's on Multivariate Input AND Multivariate OutputKeras importing two images of same object for ConvLSTMHow to feed output of predict value back into the input using LSTM in python

When and why was runway 07/25 at Kai Tak removed?

Distinction between 地平線 【ちへいせん】 and 水平線 【すいへいせん】

What is the meaning of "You've never met a graph you didn't like?"

Why do Radio Buttons not fill the entire outer circle?

How to test the sharpness of a knife?

Air travel with refrigerated insulin

What does the word 'upstream' mean in the context?

Why didn't Voldemort know what Grindelwald looked like?

Asserting that Atheism and Theism are both faith based positions

spatio or spatial

What is this high flying aircraft over Pennsylvania?

How do I prevent inappropriate ads from appearing in my game?

Do people actually use the word "kaputt" in conversation?

Limit max CPU usage SQL SERVER with WSRM

How do i tell my boss that i'm quitting in 15 days (a colleague left this week)

If the only attacker is removed from combat, is a creature still counted as having attacked this turn?

How would a solely written language work mechanically

Confusion over Hunter with Crossbow Expert and Giant Killer

How to make a list of partial sums using forEach

Isometric embedding of a genus g surface

Remove all of the duplicate numbers in an array of numbers - Javascript

Why the "ls" command is showing the permissions of files in a FAT32 partition?

Are hand made posters acceptable in Academia?

Is there a distance limit for minecart tracks?



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


Tensorflow neural network TypeError: Fetch argument has invalid typeImage as input and output in kerasHow do I provide input and output for such a network structure in kerasTensorFlow: Regression using Deep Neural NetworkMatrix Factorisation ImprovementHow to use the time-sampled data(50 samples/Minute) as input for classifying the outputSeparate scalers for input and outputUsing LSTM's on Multivariate Input AND Multivariate OutputKeras importing two images of same object for ConvLSTMHow to feed output of predict value back into the input using LSTM in python













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
    Mar 12 at 14:27











  • $begingroup$
    @mapto, thank you - this does work! Appreciate your feedback.
    $endgroup$
    – Chris Tennant
    Mar 12 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 at 16:54










  • $begingroup$
    @n1k31t4, yeah I just want to count files in a directory. ct is a string that contains wildcard characters.
    $endgroup$
    – Chris Tennant
    Mar 13 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 at 17:29















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
    Mar 12 at 14:27











  • $begingroup$
    @mapto, thank you - this does work! Appreciate your feedback.
    $endgroup$
    – Chris Tennant
    Mar 12 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 at 16:54










  • $begingroup$
    @n1k31t4, yeah I just want to count files in a directory. ct is a string that contains wildcard characters.
    $endgroup$
    – Chris Tennant
    Mar 13 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 at 17:29













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 Mar 12 at 14:20









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
    Mar 12 at 14:27











  • $begingroup$
    @mapto, thank you - this does work! Appreciate your feedback.
    $endgroup$
    – Chris Tennant
    Mar 12 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 at 16:54










  • $begingroup$
    @n1k31t4, yeah I just want to count files in a directory. ct is a string that contains wildcard characters.
    $endgroup$
    – Chris Tennant
    Mar 13 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 at 17:29
















  • $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 at 14:27











  • $begingroup$
    @mapto, thank you - this does work! Appreciate your feedback.
    $endgroup$
    – Chris Tennant
    Mar 12 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 at 16:54










  • $begingroup$
    @n1k31t4, yeah I just want to count files in a directory. ct is a string that contains wildcard characters.
    $endgroup$
    – Chris Tennant
    Mar 13 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 at 17:29















$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 at 14:27





$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 at 14:27













$begingroup$
@mapto, thank you - this does work! Appreciate your feedback.
$endgroup$
– Chris Tennant
Mar 12 at 14:58




$begingroup$
@mapto, thank you - this does work! Appreciate your feedback.
$endgroup$
– Chris Tennant
Mar 12 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 at 16:54




$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 at 16:54












$begingroup$
@n1k31t4, yeah I just want to count files in a directory. ct is a string that contains wildcard characters.
$endgroup$
– Chris Tennant
Mar 13 at 17:20




$begingroup$
@n1k31t4, yeah I just want to count files in a directory. ct is a string that contains wildcard characters.
$endgroup$
– Chris Tennant
Mar 13 at 17:20




1




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 at 17:29




$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 at 17:29










1 Answer
1






active

oldest

votes


















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





share|improve this answer











$endgroup$












  • $begingroup$
    Thanks @n1k31t4, this does exactly what I want. Much appreciated!
    $endgroup$
    – Chris Tennant
    Mar 15 at 0:26










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









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





share|improve this answer











$endgroup$












  • $begingroup$
    Thanks @n1k31t4, this does exactly what I want. Much appreciated!
    $endgroup$
    – Chris Tennant
    Mar 15 at 0:26















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





share|improve this answer











$endgroup$












  • $begingroup$
    Thanks @n1k31t4, this does exactly what I want. Much appreciated!
    $endgroup$
    – Chris Tennant
    Mar 15 at 0:26













1












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





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








edited 2 days ago

























answered Mar 13 at 17:34









n1k31t4n1k31t4

6,3912319




6,3912319











  • $begingroup$
    Thanks @n1k31t4, this does exactly what I want. Much appreciated!
    $endgroup$
    – Chris Tennant
    Mar 15 at 0:26
















  • $begingroup$
    Thanks @n1k31t4, this does exactly what I want. Much appreciated!
    $endgroup$
    – Chris Tennant
    Mar 15 at 0:26















$begingroup$
Thanks @n1k31t4, this does exactly what I want. Much appreciated!
$endgroup$
– Chris Tennant
Mar 15 at 0:26




$begingroup$
Thanks @n1k31t4, this does exactly what I want. Much appreciated!
$endgroup$
– Chris Tennant
Mar 15 at 0:26

















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

Marja Vauras Lähteet | Aiheesta muualla | NavigointivalikkoMarja Vauras Turun yliopiston tutkimusportaalissaInfobox OKSuomalaisen Tiedeakatemian varsinaiset jäsenetKasvatustieteiden tiedekunnan dekaanit ja muu johtoMarja VaurasKoulutusvienti on kestävyys- ja ketteryyslaji (2.5.2017)laajentamallaWorldCat Identities0000 0001 0855 9405n86069603utb201588738523620927

Which is better: GPT or RelGAN for text generation?2019 Community Moderator ElectionWhat is the difference between TextGAN and LM for text generation?GANs (generative adversarial networks) possible for text as well?Generator loss not decreasing- text to image synthesisChoosing a right algorithm for template-based text generationHow should I format input and output for text generation with LSTMsGumbel Softmax vs Vanilla Softmax for GAN trainingWhich neural network to choose for classification from text/speech?NLP text autoencoder that generates text in poetic meterWhat is the interpretation of the expectation notation in the GAN formulation?What is the difference between TextGAN and LM for text generation?How to prepare the data for text generation task

Is this part of the description of the Archfey warlock's Misty Escape feature redundant?When is entropic ward considered “used”?How does the reaction timing work for Wrath of the Storm? Can it potentially prevent the damage from the triggering attack?Does the Dark Arts Archlich warlock patrons's Arcane Invisibility activate every time you cast a level 1+ spell?When attacking while invisible, when exactly does invisibility break?Can I cast Hellish Rebuke on my turn?Do I have to “pre-cast” a reaction spell in order for it to be triggered?What happens if a Player Misty Escapes into an Invisible CreatureCan a reaction interrupt multiattack?Does the Fiend-patron warlock's Hurl Through Hell feature dispel effects that require the target to be on the same plane as the caster?What are you allowed to do while using the Warlock's Eldritch Master feature?