0

I'm trying to use listdir() to get filename like: "SPRING4800-D7-V40-H70.png". However, i'm only getting the name of the folder above that, so for example "SPRING4800". I cannot just type in '/content/images/maleset/SPRING4800' because the name changes as I have many of these folders. Pls see below example of filenames, thanks!

l2=os.listdir('/content/images/maleset/') 

  inflating: /content/images/maleset/SPRING4799/SPRING4799-D7-V40-H80.png  
  inflating: /content/images/maleset/SPRING4799/SPRING4799-D7-V40-H90.png  
  inflating: /content/images/maleset/SPRING4800/SPRING4800-D7-V0-H0.png  
  inflating: /content/images/maleset/SPRING4800/SPRING4800-D7-V0-H10.png  
  inflating: /content/images/maleset/SPRING4800/SPRING4800-D7-V0-H100.png  
  inflating: /content/images/maleset/SPRING4800/SPRING4800-D7-V0-H110.png  
  inflating: /content/images/maleset/SPRING4800/SPRING4800-D7-V0-H120.png  
  • So you're asking how you can list files using `os.listdir` recursively? – Grismar Nov 21 '21 at 07:27
  • Does this answer your question? [Python recursive folder read](https://stackoverflow.com/questions/2212643/python-recursive-folder-read) – Grismar Nov 21 '21 at 07:27
  • its doing it recursively already, but its not giving what I want. Currently, it only gives me name of all folders e.g. "SPRING4800", "SPRING4801", "SPRING4802" . But what I really need is the filename which is one level below that eg "SPRING4800-D7-V0-H120.png " – lingyau lee Nov 21 '21 at 07:31
  • Does this answer your question? https://stackoverflow.com/questions/39909655/listing-of-all-files-in-directory – John Byro Nov 21 '21 at 07:40
  • this thing works but it shuffles the data around which I cannot really use . This data is actually part of a pandas df which I am inputting into datagen.flow_from_dataframe in the parameter x_col='name', which is referring to these filenames from the pd df. So the data cannot be shuffled around. – lingyau lee Nov 21 '21 at 07:57

0 Answers0