-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlocate_file.py
28 lines (23 loc) · 1.07 KB
/
locate_file.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# -*- coding: utf-8 -*-
"""
Recursively search for a file in a folder and all it's subfolders.
Print out the locations where that file is located.
Note: Tested on Windows. The seperate value in split function will need to change based on different OS.
"""
import os
def search_file(folder_path, file_name):
for dirpath, dirs, files in os.walk(folder_path):
path = dirpath.split('/')
base_folder_path = folder_path.split("\\")
for paths in path:
sub_folders=paths.split("\\")
print ('|', (len(sub_folders)-len(base_folder_path))*'-', '[',os.path.basename(dirpath),']') #print folders
for f in files:
if f.find(file_name) != -1: #if file in a folder matches the file to locate, print it
print ('|', (len(sub_folders)-len(base_folder_path)+1)*'-', f)
def main():
folder_path = "." # Default path is where this script is located.
file_name = "locate_file" #Default filename to locate is this python script
search_file(folder_path,file_name)
if __name__ == "__main__":
main()