Finding a directory for folders and files using python

I have a directory structure as shown below.

MainFolder | [lib] / | \ [A] [B] [C] -- file1.so | | file2.so file1.so file1.so file2.so file2.so 

I am trying to find the "lib" folder in this structure, which may sometimes be missing. Therefore, I use the following to check for the existence of the "lib" folder:

  if os.path.isdir(apkLocation + apkFolder + '/lib/'): 

If the lib folder exists, I continue to search for folders inside 'lib'. I have to store the folder names A, B and C and look for files ending in ".so", the path of which should be stored as / lib / A / file 1.so, / lib / A / file2.so and so on.

  if os.path.isdir(apkLocation + apkFolder + '/lib/'): for root, dirs, files in os.walk(apkLocation + apkFolder): for name in files: if name.endswith(("lib", ".so")): print os.path.abspath(name) 

It gives me a way out

  file1.so file2.so file1.so file2.so file1.so file2.so 

Required Conclusion:

  /lib/A/file1.so /lib/A/file2.so /lib/B/file1.so /lib/B/file2.so /lib/C/file1.so /lib/C/file2.so 

as well as folders A, B and C must be saved separately.

+4
source share
1 answer

You need to join the current directory and name to get the absolute path to the file:

 for root, dirs, files in os.walk(apkLocation + apkFolder): for name in files: if name.endswith(("lib", ".so")): os.path.join(root, name) 

It also describes http://docs.python.org/3/library/os.html#os.walk .

+9
source

Source: https://habr.com/ru/post/1502692/


All Articles