Reputation: 125
I am using python 2.7 and trying to search folder not file.
We have specific project number for every project and evry documents goes to project folder under any project. So , we have to search the project folder if it is exist or valid.
My script can find the specific folder but it takes long time (about 11 to 13 minutes to complete the process)
I would like to share my script work to have some better ideas.
import os
path=r"F:\Projekte"
def find_directory():
for root, dirs, files in os.walk(path):
for i in dirs:
if i=="A9901001":
print root
find_directory()
Upvotes: 1
Views: 60
Reputation: 16733
Why not execute linux find
command or tree
commands (FIND
in case of windows) in python . Commands like find
would use system indexes and probably give a faster result.
Something like:-
find . -type d -name "A9901001"
Python code should look something like this
import os
paths = os.system('find . -type d -name "A9901001"')
Note:- Follow this solution only if you are getting some performance, since your initial approach is certainly more elegant.
Upvotes: 1
Reputation: 46849
why no just this?
def find_directory():
for root, dirs, files in os.walk(path):
if "A9901001" in dirs:
print(root, "A9901001")
and if you have a chace to use python3: there is os.scandir
which does similar things as os.walk
but is a lot faster on NTFS.
Upvotes: 1