0

I have a bunch of folders, each containing a set and equal number of files. I want to use python to loop through each folder and do some analysis to each file. I want to store the results of the analysis within a numpy array.

For example, suppose we have 3 folders, each containing 5 files. I want the analysis results to be stored within an array results=np.zeros((3,5))

Here's a code snippet close to what I want, but not correct.

results=np.zeros((3,5))
dircount=0
filecount=0
for root, dirs, files in os.walk(ROOTFOLDER):
  for dir in root:
    for file in dirs:
      result[dircount,filecount]=#do some analysis with file
      filecount=filecount+1
    dircount=dircount+1
    filecount=0
print result

I must confess, I do not fully understand how os.walk works, but it seems good for jobs involving a loop through files and folders.

2 Answers 2

1

os.walk() does the most part of what you're trying to do manually:

results=np.zeros((3,5))
dircount=0
for root, dirs, files in os.walk(ROOTFOLDER):
    filecount = 0
    for f in files:
        #  absolute filename is os.path.join(root, f)
        result[dircount,filecount] = #do some analysis with file
        filecount += 1
    dircount += 1       
print result

The main loop will walk down all folders in dirs recursively, and thus you get all files in that folder tree.

Sign up to request clarification or add additional context in comments.

Comments

1

You can use os.walk here

results=np.zeros((3,5))
dircount=0
for root, dirs, files in os.walk(ROOTFOLDER):
    filecount = 0
    for f in files:
        #  absolute path is os.path.join(root,f)
        result[dircount,filecount]
        filecount += 1
    dircount += 1       

print result

If you need complete files_list in the folder reccursively

files_list = [os.path.join(folder,i) for folder, subdirs, files in os.walk(ROOTFOLDER) for i in files]

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.