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

Adding axes to figuresAdding axes labels to LaTeX figuresLaTeX equivalent of ConTeXt buffersRotate a node but not its content: the case of the ellipse decorationHow to define the default vertical distance between nodes?TikZ scaling graphic and adjust node position and keep font sizeNumerical conditional within tikz keys?adding axes to shapesAlign axes across subfiguresAdding figures with a certain orderLine up nested tikz enviroments or how to get rid of themAdding axes labels to LaTeX figures

Tähtien Talli Jäsenet | Lähteet | NavigointivalikkoSuomen Hippos – Tähtien Talli

Do these cracks on my tires look bad? The Next CEO of Stack OverflowDry rot tire should I replace?Having to replace tiresFishtailed so easily? Bad tires? ABS?Filling the tires with something other than air, to avoid puncture hassles?Used Michelin tires safe to install?Do these tyre cracks necessitate replacement?Rumbling noise: tires or mechanicalIs it possible to fix noisy feathered tires?Are bad winter tires still better than summer tires in winter?Torque converter failure - Related to replacing only 2 tires?Why use snow tires on all 4 wheels on 2-wheel-drive cars?