python - Second os.walk runs extremely slow -
the program works when performs second os.walk
operation takes longer first time. first time os.walk
occurs walks through directory tree until finds directory name matching partno
input. stores directory location path_to_files
. walks given directory , opens files start partno
. guess after finding directory on first os.walk
continues searching make sure doesn't find matching directory. said works great seems find path_to_files
directory instantly when runs open files takes longer (8-15 seconds) there doing wrong in code or have attempting open 3-6 files in different applications @ once? appreciated. in advance...
import os partno ="" partno = raw_input("enter part no:") #takes user input of part no yourpath = r"y:\shop\manufacturing docs" #parent directory print ("loading part no %r files...please wait") % partno root, dirs, files in os.walk(yourpath, topdown=true): name in dirs: if name == partno: #matches folder in directory `part no` path_to_files = (os.path.abspath(os.path.join(root, name))) #stores path in variable files found in folder print path_to_files #confirmation line me dname, _, fnames in os.walk(path_to_files, topdown=true): fname in fnames: if fname.startswith(partno): filepath = (os.path.abspath(os.path.join(dname, fname))) os.startfile(filepath) #opens files in folder in native applications print filepath break raise systemexit
Comments
Post a Comment