i need help i thought i was doing it right but later when i looked at data it turned out to be something else

I have 4 lines of code that i want to convert to single line list comprehension

  `newLst=[]
  for each in os.listdir(locationTxt):
          if os.path.isdir(os.path.join(locationTxt,each)):
             if each not in hideTheseFolders:#hide directory in scrolllist
                newLst.append(each)
             for nfile in ["ma","jpg","png","mb",'iff','tga','tif']:
              if each.endswith(nfile):
                 newLst.append=self.sizeof(os.path.join(locationTxt,each)))`

and I want to write in just one line,

`for  each in [each for each in os.listdir(locationTxt) if os.path.isdir(os.path.join(locationTxt,each)) and each not in hideTheseFolders]:
              newLst.append(each)
for nfile in ["ma","jpg","png","mb",'iff','tga','tif']:
              if each.endswith(nfile):
                 newLst.append=self.sizeof(os.path.join(locationTxt,each)))`

and hideTheseFolders is a list that has...

 ["thumb",".thumb",".mayaSwatches","RECYCLER","$AVG","$Recycle.Bin","$RECYCLE.BIN","INCINERATE","Config.Msi","System Volume Information","Recovery","ProgramData","PROGRAMDATA","PerfLogs"]

but instead of just filtering out these folders I end up with just the folders and no file is visible..

pls help adding files of the jpg, tif or png and the folders to the newLst list...

Recommended Answers

All 3 Replies

# add jpg, tif or png and the folders to the newLst
newLst += [file for file in thisdir if os.path.isdir(file) or file.endswith(('.jpg', '.tif', '.png'))]

what makes u think it works.?

Testing.

import os
newLst = []
thisdir = os.listdir('.')
# add jpg, tif or png and the folders to the newLst
newLst += [file for file in thisdir if os.path.isdir(file) or file.endswith(('.jpg', '.tif', '.png'))]
print newLst
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.