Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
nested for loops glob
#1
I'm trying to use a for loop and a nested for loop
So what I have is:

for f in glob("/Users/Desktop/network5"):
         for x in glob(f/NET_00*):
              print x
         break
    break
This doesn't give me any output.

I realize if I use glob I probably can't use the "f" I defined earlier in the for loop. But how can I use the same path that is assigned to f to use in the nested for loop, x?

For the nested loop, x, I wanted it to loop through all the files in that directory.
Reply
#2
Walking through the files in directory:

import glob
import os


for path in glob.glob("/path/to/dir/*"):
    if os.path.isfile(path):
        print(path)
You must put * to the end of the path to walk through the all files and sub-directories.

You can use the path to from the first loop - it is simple string. Here is the example of printing files from all path's sub-directories:

import glob
import os


for path in glob.glob("/path/to/dir/*"):
    if os.path.isdir(path):
        for sub_path in glob.glob(path + "/*"):
            if os.path.isfile(sub_path):
                print(sub_path)
Reply
#3
Thank you so much. But I had one more question that's slightly more difficult to answer perhaps.
That is, what if I wanted the first for loop to cycle through network(5-10) and then beneath that, add a nested loop that loops within each folder to extract all the files?

import glob 
import os
for f in glob("/Users/Desktop/network*"):
         for x in glob(f/NET_00*):
              print x
         break
    break
So basically if we are looking through folder, "network6", I want it to loop through all files and once its done looping through all files within network6 to then go on to network7 and loop through all the files in network 7, and so on and so forth.

NOTE: so the difference this time is that the first loop is also required to loop through the set of files
Reply
#4
Well if you know the range of the networks, you can do it like this:

import glob
import os


# let's iterate between number 5 and 10
for network_number in range(5, 11):
    # converting network number to string with leading zero for number < 10
    network_number = str(network_number).zfill(2)
    # iterating files in particular network directory
    for path in glob.glob("/Users/Desktop/network/NET_" + network_number + "/*"):
        if os.path.isfile(path):
            print(path)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  reduce nested for-loops Phaze90 11 1,763 Mar-16-2023, 06:28 PM
Last Post: ndc85430
  Nested for loops: Iterating over columns of a DataFrame to plot on subplots dm222 0 1,641 Aug-19-2022, 11:07 AM
Last Post: dm222
  Nested for loops - help with iterating a variable outside of the main loop dm222 4 1,532 Aug-17-2022, 10:17 PM
Last Post: deanhystad
  breaking out of nested loops Skaperen 3 1,175 Jul-18-2022, 12:59 AM
Last Post: Skaperen
  |SOLVED] Glob JPGs, read EXIF, update file timestamp? Winfried 5 2,413 Oct-21-2021, 03:29 AM
Last Post: buran
  Break out of nested loops muzikman 11 3,243 Sep-18-2021, 12:59 PM
Last Post: muzikman
  [SOLVED] Input parameter: Single file or glob? Winfried 0 1,540 Sep-10-2021, 11:54 AM
Last Post: Winfried
  q re glob.iglob iterator and close jimr 2 2,180 Aug-23-2021, 10:14 PM
Last Post: perfringo
  How to break out of nested loops pace 11 5,264 Mar-03-2021, 06:25 PM
Last Post: pace
  Nested for Loops sammay 1 7,328 Jan-09-2021, 06:48 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020