Python: read folders that are not ZIP folders

Viewed 37

Currently I read folders individually like so:

input_location = r'path\202206'

I would like to read in all the folders at once + not read in the zipped files.

enter image description here

Essentially the logic id like to perform is input_location = r'path\202206' + r'path\202207' + r'path\202207' + r'path\202208'

I cant just do input_location = r'path\ as it may read in those zip files I do not want.

Is there a way to read in the folders without reading in the zip files? Or explicitly list the folder names in one variable (input_location)?

3 Answers

IIUC: Collecting all directories from a directory can be done using a simple list comprehension as follows.

The glob library is nice, as it returns the full filepath, whereas a function such as os.listdir() only returns the filenames.

import os
from glob import glob

dirs = [f for f in glob('/path/to/files/*') if os.path.isdir(f)]

Output:

['/path/to/files/202207', 
 '/path/to/files/202206', 
 '/path/to/files/202208',  
 '/path/to/files/202209']    

Then, your script can iterate over the list of directories as required.


For completeness, the directory content is a follows:

202206
202206.zip
202207
202207.zip
202208
202208.zip
202209
202209.zip

If you use pathlib from Pythons standard library you can get all entries in the folder and check if the entry is a folder.

from pathlib import Path

for entry in Path('/path/to/folder').glob('*'):
    if entry.is_dir():
        print(entry)

A os base approach. Notice that os.listdir returns the content of the directory in a basename form and not as a path.

import os

def my_dirs(wd):
    return list(filter(os.path.isdir, (os.path.join(wd, f) for f in os.listdir('.'))))

working_dir = # add path

print(*my_dirs(working_dir), sep='\n')

Remarks: to make your program platform independent you always stuffs like os.path.join or os.sep for path manipulation

Related