0

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)?

Jonnyboi
  • 505
  • 5
  • 19
  • Is this question sufficient "Is there a way to read in the folders without reading in the zip files?" or is more detail needed? – Jonnyboi Sep 08 '22 at 14:18
  • 1
    Could you show the code you use to read the entries in the folder? – Matthias Sep 08 '22 at 14:21
  • @Matthias , I currently just read in 1 folder at a time , `input_location = r'path\202206' `. I dont read them in at once. I have to run my script 4 times to get desired result right now. – Jonnyboi Sep 08 '22 at 14:22
  • @Jonnyboi could you not just turn that into a loop? If you google "python read all files in directory" you get [this](https://stackoverflow.com/q/3207219/12479639) as your top result with quite a few answers. From there it seems like your only question would be "How do I determine if a string ends with ".zip"?". Hint: `str.endwith('.zip')` – Axe319 Sep 08 '22 at 14:26
  • @Axe319 `os.listdir` and `glob` was OK at the time the answer was written. Nowadays you would use `pathlib`. – Matthias Sep 08 '22 at 14:31

3 Answers3

2

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
S3DEV
  • 8,768
  • 3
  • 31
  • 42
1

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)
Matthias
  • 12,873
  • 6
  • 42
  • 48
1

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

cards
  • 3,936
  • 1
  • 7
  • 25