0

Let me start by saying I'm fairly new to python.

Ok so I'm running code to perform physics calculations/draw graphs etc on data files, and what I need to do is loop over a number of files and sub-files. But the problem is there are a different number of sub-files in each file (e.g. file 0 has 711 sub-files, file 1 has 660 odd). It obviously doesn't like it when I run it across a file that doesn't have a sub-file at index x, so I was wondering is there a way to get it to run (iterate?) up to the final limit in each file automatically?

What I've got is a nested loop like:

for i in range(0,120):
    for j in range(0,715):

         stuff

Cheers in advance for any help, and sorry if my explanation is bad!

Edit: some more of the code. So what I'm actually doing is calculating/plotting the angular momentum of gas and dark matter particles. These are in halos (j), & there are a number of files (i) containing lots and lots of these halos.

import getfiby
import numpy as np 
import pylab as pl


angmom=getfiby.ReadFIBY("B")

for i in range(0,120):
    for j in range(0,715):

        pos_DM = angmom.getParticleField(49,i,j,"DM","Coordinates")
        vel_DM = angmom.getParticleField(49,i,j,"DM","Velocity")
        mass_DM = angmom.getParticleField(49,i,j,"DM","Mass")
        more stuff

getfiby is a code I was given that retrieves all the data from the files (that I can't see). It's not really a massive problem, as I can still get it to run the calculations & make plots even if the upper limit I put on my range for j exceeds the number of halos there are in a particular file (I get: Halo index out of bounds. Goodbye.) But yeah I just wondered if there was a nicer, tidier way of getting it to run.

4
  • 5
    It would help if we could see a bit more of the code for context. Commented Feb 27, 2014 at 15:34
  • What`s a sub file? Are you asking how to iterate of the files in a directory and its subdirectories? Commented Feb 27, 2014 at 15:37
  • 1
    Are the files folders or ZIPs or some other format of files? Commented Feb 27, 2014 at 15:37
  • 1
    As a side note, changing those range to xrange will perform better under python2. If it's python3, leave it as is. Commented Feb 27, 2014 at 15:38

4 Answers 4

1

You may want something like this:

a=range(3)
for i in range(5):
    try:
        b=a[i] #if you iterate over your "subfiles" by index
    except IndexError:
        break #break out when list index out of range
Sign up to request clarification or add additional context in comments.

Comments

0

When you build the list of files and subfiles to process, you could have the list in a variable named filelist and the subfile list in a variable called subfilelist You could then run the loops as

for myfile in filelist:
  # Process code
  for subfile in subfilelist:
    # Process code.

If you need to use a range then

filerange = len(filelist)
subfilerange = len(subfilelist)

for i in range(0, filerange):
  for j in range(0, subfilerange):

Comments

0

I may be misunderstanding your structure/intent here, but it sounds like you want to perform some calculations on each file in some folder structure, where the folders contain various numbers of the data files. In that case, I'd make use of the os.listdir function rather than trying to manually index each data file.

import os
for file in os.listdir(ROOT_DIRECTORY):
    for subfile in os.listdir(os.path.join(ROOT_DIRECTORY, file)):
        process(os.path.join(ROOT_DIRECTORY, file, subfile)) # or whatever

This can of course be made a little easier to look at in a few ways (personally I have my own listdir wrapper that returns full paths instead of just the basenames), but that would be my basic idea.

If you also need the index of each file, you can probably get it using some combination of enumerate and maybe sorted (e.g. for i, file in enumerate(sorted(os.listdir(...)))), but the specifics obviously depend on your filenames and directory structure.

Comments

0

Assuming fileSet is an iterable full of file objects, and each file object is itself an iterable full of subfile objects, than:

    for i,file in enumerate(fileSet): 
        for j,subfile in enumerate(file):
            stuff

But think hard about whether you really need the indices i and j, as you already have the words file and subfile to refer to the objects you are dealing with. If you don't need the indices, it's simply:

    for file in fileSet: 
        for subfile in file:
            stuff

Now, if by "file" you actually meant files in the Operating System's Filesystem, then I need you to explain me what a subfile is in that context, as Filesystem files usually cannot be nested, only directories can.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.