Skip to content Skip to sidebar Skip to footer

Os.listdir() Not Printing Out All Files

I've got a bunch of files and a few folders. I'm trying to append the zips to a list so I can extract those files in other part of the code. It never finds the zips. for file in os

Solution 1:

It's possible your files have more than one period in them. Try using str.endswith:

reg_zips = []
for file inos.listdir(path):
     if file.endswith('zip'):
         reg_zips.append(file)

Another good idea (thanks, Jean-François Fabre!) is to use os.path.splitext, which handles the extension quite nicely:

ifos.path.splitext(file)[-1] == '.zip':
    ... 

Am even better solution, I recommend with the glob.glob function:

importglobreg_zips= glob.glob('*.zip')

Solution 2:

reg_zips = [z for z in os.listdir(path) if z.endswith("zip")]

Post a Comment for "Os.listdir() Not Printing Out All Files"