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 _
.