0

Here is the below code we have developed for single directory of files

from os import listdir

with open("/user/results.txt", "w") as f:
    for filename in listdir("/user/stream"):
        with open('/user/stream/' + filename) as currentFile:
            text = currentFile.read()
            if 'checksum' in text:
                f.write('current word in ' + filename[:-4] + '\n')
            else:
                f.write('NOT ' + filename[:-4] + '\n')

I want loop for all directories Thanks in advance

ram reddy
  • 25
  • 1
  • 4

2 Answers2

0

If you're using UNIX you can use grep:

grep "checksum" -R /user/stream

The -R flag allows for a recursive search inside the directory, following the symbolic links if there are any.

agubelu
  • 399
  • 1
  • 7
0

My suggestion is to use glob.

The glob module allows you to work with files. In the Unix universe, a directory is / should be a file so it should be able to help you with your task.

More over, you don't have to install anything, glob comes with python.

Note: For the following code, you will need python3.5 or greater


This should help you out.

import os
import glob

for path in glob.glob('/ai2/data/prod/admin/inf/**', recursive=True):
    # At some point, `path` will be `/ai2/data/prod/admin/inf/inf_<$APP>_pvt/error`
    if not os.path.isdir(path):
        # Check the `id` of the file
        # Do things with the file
        # If there are files inside `/ai2/data/prod/admin/inf/inf_<$APP>_pvt/error` you will be able to access them here

What glob.glob does is, it Return a possibly-empty list of path names that match pathname. In this case, it will match every file (including directories) in /user/stream/. If these files are not directories, you can do whatever you want with them.

I hope this will help you!


Clarification

Regarding your 3 point comment attempting to clarify the question, especially this part we need to put appi dynamically in that path then we need to read all files inside that directory

No, you do not need to do this. Please read my answer carefully and please read glob documentation.

In this case, it will match every file (including directories) in /user/stream/

If you replace /user/stream/ with /ai2/data/prod/admin/inf/, you will have access to every file in /ai2/data/prod/admin/inf/. Assuming your app ids are 1, 2, 3, this means, you will have access to the following files.

/ai2/data/prod/admin/inf/inf_1_pvt/error
/ai2/data/prod/admin/inf/inf_2_pvt/error
/ai2/data/prod/admin/inf/inf_3_pvt/error

You do not have to specify the id, because you will be iterating over all files. If you do need the id, you can just extract it from the path.

If everything looks like this, /ai2/data/prod/admin/inf/inf_<$APP>_pvt/error, you can get the id by removing /ai2/data/prod/admin/inf/ and taking everything until you encounter _.

mayk93
  • 1,489
  • 3
  • 17
  • 31
  • @ramreddy Don't forget to select this answer as the correct one if you find it useful! – mayk93 May 10 '18 at 10:04
  • thanks for response my requirement is we have app ID's by using those id's we need to go directory and read files for a word for single directory I have done I m asking for loop directory structure is "/ai2/data/prod/admin/inf/inf_<$APP>_pvt/error" please help me – ram reddy May 10 '18 at 10:08
  • You are not expressing yourself clearly. I do not understand what you are trying to do. The code I gave you gives you access to **ALL** files inside `/user/stream/`. You can check for the `id` as you iterate over. Can you try to explain more clearly what you are trying to do? – mayk93 May 10 '18 at 10:12
  • 1.we will get appids 2.directory structure is /ai2/data/prod/admin/inf/inf_<$APP>_pvt/error 3.according appid we need to put appi dynamically in that path then we need to read all files inside that directory – ram reddy May 10 '18 at 10:22
  • @ramreddy The scenario you are describing works with the provided answer. – mayk93 May 10 '18 at 10:38
  • thank you we have go through every directory acoording to appid only – ram reddy May 10 '18 at 10:50
  • @ramreddy You are not understanding the answer and you are most likely not understanding your own task. I don't know what your requirements are but `have go through every directory acoording to appid only` is not part of it, I guarantee it. Please read the question you asked and my answer again. – mayk93 May 10 '18 at 10:59