简体   繁体   中英

How to loop through files in a directory multiple times in python

I'm trying to input data from a range of specific files in a large directory into a model written in Python.

To loop through files 1 time I currently do:

for i in range(nt):
    streamflow = "streamflow"
    nc = ".nc"
    discharge_filename=[streamflow + `i` +nc for i in range(1000,2000)]

Then use each of these files 1 time for every time step in the model. I want to be able to loop through the files more than once, so when I reach, discharge_filename = streamflow2000.nc, I return back to streamflow1000.nc and start over counting until the for loop is finished.

This seems simple but I rarely code in Python and this is really stumping me! Any help is appreciated, thanks!

If you're looking to loop over what you've already looped over, you need to define an outer loop. The outer loop will define how many times that you'd like your inner loop to run.

For example:

for n in range(0,10):
  for o in range(0,10):
    print(n + o)

@Chadvandehey's answer is correct - if you want to go over infinite loop just do while True or maybe without outer loop at all : just use xrange instead of range and assign value of 0 in the inner loop when i == nt-1 . use while loop , something like this:

 i = 0 
 while i < nt:
    //do whatever you want
    if i == nt-1:
        i=0

just be very careful with infinite loops.

In Python 3.6+ you can use Literal String Interpolation . Creating filenames in a loop can me done in the following way:

for i in range(1000, 2001):
    filename = f'streamflow{i}.nc'
    ...

Regarding the other answers proposing putting an outer loop around the code above, I think that you actually want to do sth like that:

for i in range(1000, 2001):
    filename = f'streamflow{i}.nc'
    # read data

# Some logic here

for i in range(1000, 2001):
    filename = f'streamflow{i}.nc'
    # write data (once to each file)

However, if you really want/need to append some data to these files numerous times, just use an outer loop as offered in the other answers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM