Bottom Page

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.
Quote
#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)
Quote
#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
Quote
#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)
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Nested for loops d79danny 8 191 Oct-05-2019, 02:12 AM
Last Post: newbieAuggie2019
  Version of glob for that Supports Windows Wildcards? Reverend_Jim 5 534 Jun-18-2019, 06:31 PM
Last Post: Reverend_Jim
  Printing out a triangle using nested for loops MrGoat 12 770 Jan-16-2019, 07:21 PM
Last Post: ichabod801
  Looping and nested loops albry 5 644 Dec-20-2018, 12:10 PM
Last Post: buran
  nested for loops to recursion ashkea26 1 477 Nov-02-2018, 09:53 AM
Last Post: Larz60+
  Glob and automating help Thunberd 0 512 Jun-13-2018, 04:42 PM
Last Post: Thunberd
  Question on runtime and improving nested for loops ackmondual 1 1,198 Jun-13-2017, 11:11 PM
Last Post: ichabod801
  Adding regedit value to glob.glob AlterBlitz 2 1,175 May-18-2017, 09:09 PM
Last Post: nilamo
  glob for dir listing bluefrog 5 1,610 Mar-07-2017, 05:46 PM
Last Post: bluefrog

Forum Jump:


Users browsing this thread: 1 Guest(s)